Compartilhar via


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çãoObservaçã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

  1. Create a new WPF Application project named "DemoApplication". For more information, see Como: Criar um novo projeto de aplicativo WPF.

  2. 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.

  3. On the Build menu, select Build Solution to build the solution.

  4. Abra MainWindow. XAML no designer.

  5. In XAML view, in the opening Window tag, insert a new line following the second xmlns mapping.

  6. 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>
    
  7. Após a tag de abertura de Grid elemento, tipo de < dc: e selecione DemoControl na lista de IntelliSense .

  8. 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

  1. 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.

  2. 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.

  3. On the Build menu, select Build Solution to build the solution.

  4. Abra MainWindow. XAML no designer.

  5. In XAML view, in the opening Window tag, insert a new line following the third xmlns mapping.

  6. 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>
    
  7. Após a tag de fechamento do DemoControl elemento, tipo de < vc: e selecione UserControl1 na lista de IntelliSense .

  8. 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

Outros recursos

Namespace XAML (x:) Recursos da linguagem

Guia de Introdução com o WPF Designer