Como: Importar um namespace em XAML
To use your custom controls and third-party controls in XAML, you need to import namespaces and reference assemblies. For more information, see Namespaces XAML e o mapeamento de Namespace para WPF XAML.
Observação |
---|
Caixas de diálogo e comandos de menu que você vê podem diferir das descritas na Help dependendo das suas configurações ativas ou edição. Para alterar as configurações, escolha Import and Export Settings sobre o Ferramentas menu. For more information, see Trabalhando com configurações. |
Importing a local namespace in XAML
Create a new WPF Application project named "DemoApplication". For more information, see Como: Criar um novo projeto de aplicativo WPF.
Add a new User Control (WPF) item named "DemoControl.xaml" to the DemoApplication project. For more information, see Como: Adicionar novos itens a um projeto WPF.
On the Build menu, select Build Solution to build the solution.
Abra MainWindow. XAML no designer.
In XAML view, in the opening Window tag, insert a new line following the second xmlns mapping.
Type xmlns:dc= and select DemoApplication in assembly DemoApplication from the IntelliSense list.
The designer inserts a namespace mapping for the DemoApplication namespace.
<Window x:Class="DemoApplication.MainWindow" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:dc="clr-namespace:DemoApplication" Title="MainWindow" Height="300" Width="300"> <Grid> </Grid> </Window>
Após a tag de abertura de Grid elemento, tipo de < dc: e selecione DemoControl na lista de IntelliSense .
Type a closing bracket /> to close the element.
The Grid element should look like the following:
<Grid> <dc:DemoControl /> </Grid>
Importing a third-party namespace in XAML
Add a new WPF User Control Library project named "VendorControlLibrary" to the DemoApplication solution. When the DemoApplication solution is built, an assembly is created for each project in the solution. For more information, see Como: Criar um projeto de biblioteca UserControl WPF.
In the DemoApplication project, add a project reference to the VendorControlLibrary project. For more information, see Como: Adicionar ou remover referências no Visual Studio.
On the Build menu, select Build Solution to build the solution.
Abra MainWindow. XAML no designer.
In XAML view, in the opening Window tag, insert a new line following the third xmlns mapping.
Type xmlns:vc= and select VendorControlLibrary in assembly VendorControlLibrary from the IntelliSense list.
Intellisense inserts a namespace mapping for the VendorControlLibrary namespace, which is defined in the VendorControlLibrary.dll assembly.
<Window x:Class="DemoApplication.MainWindow" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:dc="clr-namespace:DemoApplication" xmlns:vc="clr-namespace:VendorControlLibrary;assembly=VendorControlLibrary" Title="MainWindow" Height="300" Width="300"> <Grid> <dc:DemoControl /> </Grid> </Window>
Após a tag de fechamento do DemoControl elemento, tipo de < vc: e selecione UserControl1 na lista de IntelliSense .
Type a closing bracket /> to close the element.
The Grid element should look like the following:
<Grid> <dc:DemoControl /> <vc:UserControl1 /> </Grid>
Consulte também
Conceitos
Namespaces XAML e o mapeamento de Namespace para WPF XAML