x:Class, x:Key

x:Class:

用来创建一个partial的class, 比如默认生成的x:Class="MyTest.MainWindow", 会自动生成一个MainWindow的partial class,同时在project自动创建的MainWindow.cs partial class构造函数中,会调用一个InitializeComponent() (定义在x:Class自动生成的MainWindow.g.i.cs中)。

如果注释掉InitializeComponent(), 同时把x:Class改成如MyTest.MainWindowABC, 则会生成MainWindowABC.g.i.cs,整个程序可以正常运行。查看IL,会发现有两个class, MainWindow,MainWindowABC

x:Key:

对于resource比如

<Window.Resources>
<local:Human x:Key="human" Name="Tester1" Child="ChildOfTester1"/>  同时,Human必须要有一个可以访问的无参构造函数,否则会报错
</Window.Resources>

x:Key必须要指定

原文地址:https://www.cnblogs.com/bdbw2012/p/3848542.html