Udostępnij za pośrednictwem


How to: Import a Namespace into XAML

To use your custom controls and third-party controls in XAML, you need to import namespaces and reference assemblies. For more information, see XAML Namespaces and Namespace Mapping for WPF XAML.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Working with Settings.

Importing a local namespace in XAML

  1. Create a new WPF Application project named "DemoApplication". For more information, see How to: Create a New WPF Application Project.

  2. Add a new User Control (WPF) item named "DemoControl.xaml" to the DemoApplication project. For more information, see How to: Add New Items to a WPF Project.

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

  4. Open MainWindow.xaml in the 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. After the opening tag of the Grid element, type <dc: and select DemoControl from the IntelliSense list.

  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 How to: Create a WPF UserControl Library Project.

  2. In the DemoApplication project, add a project reference to the VendorControlLibrary project. For more information, see How to: Add or Remove References in Visual Studio.

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

  4. Open MainWindow.xaml in the 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. After the closing tag of the DemoControl element, type <vc: and select UserControl1 from the IntelliSense list.

  8. Type a closing bracket /> to close the element.

    The Grid element should look like the following:

        <Grid>
            <dc:DemoControl />
            <vc:UserControl1 />
        </Grid>
    

See Also

Concepts

XAML Namespaces and Namespace Mapping for WPF XAML

Other Resources

XAML Namespace (x:) Language Features

Getting Started with the WPF Designer