Namespace in WPF XAML

Namespace in WPF XAML

开发工具与关键技术:Visual Studio 2015、WPF
作者:黄元进
撰写时间:2019年5月5日

We usually can see the beginning of the xaml file has a similar http protocol string, because it is automatically generated, did not worry so much about. But when migrating third-party control project references, often therefore lead to some mistakes, let's look at these http strings in the end represent what?

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow" Height="1080" Width="1920">
<Grid>
</Grid>
</Window>

Although it will use the clr-namespace, which is a reference to the namespace (assembly), but for the namespace URLs that begin with there may be a bit confused, it actually represents a URL? When xaml file parsed will visit this URL? If this day can not get a URL that is not our program will not run properly.
I'm here to tell you the conclusion first, then in a step by step analysis of why.
In fact, very simple to = xmlns " http://schemas.microsoft.com/winfx/2006/xaml/presentation the string, for example, in fact, System.Windows, System.Windows.Automation, System.Winjdows.Controls ... such as a collection of namespace, this collection is [alias], enter the URL in the browser sometimes inaccessible. If their definition of class libraries, I put this [alias] is called Joe Smith is also possible. Microsoft He suggested that the company is generally defined as a URL, or personal website.

  1. Decompile
    each "URL" actually represents the background of a common namespace, made a map, so we refer to different systems comes with dll component will automatically add different "URL."
    2. Customize a "URL" namespace
    to create a new class library (here named ClassLibrary1), then add a custom control, add the following sentence at the Assembly Information AssemblyInfo.cs file:
//用网址映射常规命名空间
ClassLibrary1[assembly: XmlnsDefinition("http://agriplatform.top", "ClassLibrary1")]

XmlnsDefinition System.Windows.Markup need to use the namespace, be sure to quote System.Xaml.dll, WindowsBase.dll two assemblies, otherwise there is no this function.
XAML mapping specified by naming the space between the assembly and the CLR namespace, and then write the object or XAML XAML schema context it for type resolution. Some may not need to write the whole foundation, here I put custom controls posted.

<UserControl x:Class="ClassLibrary1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="20" d:DesignWidth="30" x:Name="MyControl">
    <Grid>
        <Button x:Name="UserBtn" Width="30" Height="20" Content="你好"/>
    </Grid>
</UserControl>

3. Resolving "URL" namespace
to create a new WPF project, we are given here two kinds of reference, including the URL.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ss="http://agriplatform.top"
        xmlns:local="clr-namespace:ClassLibrary1;assembly=ClassLibrary1"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ss:UserControl1/> 
    </Grid>
</Window>

Is not see a reference to ss URLs, very simple.
Summary: xaml Web site is a map corresponding to the common namespace, can not form URL

Guess you like

Origin blog.csdn.net/weixin_44547949/article/details/89855944