wpf xamlreader.load

wpf XamlReader.Load方法允许将一个Xaml解析,也就是说如果你用这个方法可以调用另外一个Xaml的内容。
随便建一个wpf程序,新建一个wpf窗体,叫做fish.Xaml,在window1窗体中,window_load事件中,写入以下代码:

     Window window = null;
//using 是一个C#释放内存的范围定义,简单说一下,自己goog一下
       using (FileStream fs = new FileStream(@"C:\cx\fishdemo\fishdemo\fish.xaml", FileMode.Open, FileAccess.Read))
            {
                window = (Window)XamlReader.Load(fs);
            }
            Grid grid = (Grid)window.Content;
           
          // Button okbutton = (Button)penel.Children[1];
            StackPanel penel = (StackPanel)grid.Children[0];
            Button okbutton = (Button)penel.Children[1];

fish.xaml中在xaml设计中写下以下代码:

<Window x:Class="fishdemo.fish"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="fish" Height="300" Width="300">
    <Grid>
        <StackPanel x:Name="fish_panel">
            <Button x:Name="but1" Height="25" Width="60">but1</Button>
            <Button x:Name="but2" Height="25" Width="60">but2</Button>
            <Button x:Name="but3" Height="25" Width="60">but3</Button>
            <Button x:Name="but4" Height="25" Width="60">but4</Button>
        </StackPanel>
    </Grid>
</Window>

好了,运行!


虾米?
报错?为什么?他说是class属性不存在于xml命名空间中,x:Class="fishdemo.fish"这个是程序的名称,也就是所谓的命名空间,wpf中比较神秘的是如何把xml的东西映射到wpf程序当中,这只是一个人为de的
字符串,C#编程的时候,命名空间不能重复,众所周知,难道这里也是因为所谓的命名空间的重复而造成这种错误?奇怪!书本上的解释是这种一对多的映射要保证不要引入两个同样名称的类!
搜索msdn,关于x:class,<object x:Class="namespace.classname"...>
x:Class="fishdemo.fish"中,fishdemo也就是namespace,fish就是calssname,namespace是指定一个包含由 classname 标识的分部类的 CLR 命名空间。如果指定了 namespace,则用一个点 (.) 来分隔 namespaceclassname,而calssname则是 指定连接加载的 XAML 和该 XAML 的代码隐藏的分部类的 CLR 名称,也就是说,namesplace可以不要,但是classname找不到,你也就无法和后台xaml关联,也就会出现下面的错误!

{
x:Class 可以声明为充当可扩展应用程序标记语言 (XAML) 元素树的根元素并且正在编译(可扩展应用程序标记语言 (XAML) 通过 Page 生成操作包括在项目中)的任何元素的属性,也可以声明为已编译应用程序的应用程序定义中的 Application 根的属性。在页面根元素或应用程序根元素之外的任何元素上以及在未编译的可扩展应用程序标记语言 (XAML) 文件的任何环境下声明 x:Class 都会导致编译时错误。
}
msdn上已经解释的清清楚楚了,只是有点诧异,既然load方法可以加载xaml文件,那为什么不能让xml和Xaml直接进行所谓的转换,我去!gny的微软。
把x:Class="fishdemo.fish"删掉,然后运行!
      public fish()
        {
           InitializeComponent();
        }
    }
这里又会报错,嘿嘿,初始化报错,有点意思!没有了x:class,也就无法初始化,然后屏蔽!继续!
ok!程序运行正常!
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="fish" Height="300" Width="300">
    <Grid>
        <StackPanel x:Name="fish_panel">
            <Button x:Name="but1" Height="25" Width="60">but1</Button>
            <Button x:Name="but2" Height="25" Width="60">but2</Button>
            <Button x:Name="but3" Height="25" Width="60">but3</Button>
            <Button x:Name="but4" Height="25" Width="60">but4</Button>
        </StackPanel>
    </Grid>
</Window>
这是 fish.xaml的xaml代码,他的第一个元素是 grid,所以取的就是 Grid grid = (Grid)window.Content;
然后   StackPanel penel = (StackPanel)grid.Children[0];这是取grid的元素,
            Button okbutton = (Button)penel.Children[1];这是取stackpanel的元素,依次取出来,还挺有意思!另外,如果运行的时候你是无法修改 fish.xaml这个文件的,即使fish.xaml这个文件是在另外一个工程里,那时候说他是锁定的,原因是这个方法的同步和 异步,当然xamlreader也有loadasync这这个方法。

小小的东西,觉得还挺有意思!

原文地址:https://www.cnblogs.com/fish124423/p/2507375.html