# UserControls

`UserControl` 表示 Avalonia 中的一个“视图”，它是预定义布局中可重用的控件集合。

`UserControl` 通常由两部分组成：XAML文件（例如`MyUserControl.axaml`）和代码隐藏文件（例如 `MyUserControl.axaml.cs` ）。代码隐藏文件中定义了一个表示控件的 .NET 类。

在使用 MVVM 模式时，`UserControl` 通常与“视图模型”配合使用。想要了解更多信息，请浏览 [教程](https://github.com/avaloniachina/Documentation/blob/master/tutorials/todo-list-app/README.md).

你可以从模板创建 `UserControl`：

### Visual Studio <a href="#visual-studio" id="visual-studio"></a>

1. 右键单击解决方案资源管理器中要添加控件的文件夹
2. 选择 “添加->新增项”
3. 在出现的对话框中，导航到类别树中的 "Avalonia" 部分
4. 选择 “UserControl (Avalonia)”
5. 在“名称”栏输入控件名称
6. 单击“添加”按钮

### .NET Core 命令行接口 <a href="#net-core-cli" id="net-core-cli"></a>

运行这条命令，将 `[namespace]` 替换为要在其中创建 `UserControl` 的命名空间，`[name]` 替换为控件的名称。

```bash
dotnet new avalonia.usercontrol -p:n [namespace] -n [name]
```

想要了解更多信息，请浏览 [.NET core 模板代码库](https://github.com/AvaloniaUI/avalonia-dotnet-templates/).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://avaloniachina.gitbook.io/avalonia/docs/getting-started/usercontrols.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
