我的一个UserControls有一个奇怪的问题,如果我只在UserControl.Resources中使用xml命名空间,则找不到程序集。但是如果我也在控件的内容中使用相同的xml命名空间,那么两者都可以正常工作。
<UserControl x:Class="MyControl"
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"
xmlns:myAssemblyAlias="clr-namespace:MyAssembly;assembly=MyAssembly">
<UserControl.Resources>
<ControlTemplate x:Key="AControlTemplate">
<... other stuff .../>
<myAssemblyAlias:MyObject/>
</ControlTemplate>
</UserControl.Resources>
<Grid>
<myAssemblyAlias:MyObject x:Name="MyObjectInContent" />
</Grid>
</UserControl>
如果MyObject的两个实例都存在,则它可以正常工作。如果我注释掉MyObjectInContent的行,我会收到运行时错误,告诉我无法找到MyAssembly。
以前见过这样的人吗?几乎看起来Visual Studio在编译时可以正确使用引用,但不能在运行时使用。