我有一个 UserControl,我已将其设置为 Internal:
UserControl x:Class="ClassName"
x:ClassModifier="internal"
它匹配 .cs 文件:
internal partial class ClassName : UserControl
{
public ClassName()
{
InitializeComponent();
}
}
现在,当我尝试在窗口上使用控件时,如下所示:
uControls:ClassName x:Name="instanceName" Margin="0,0,8,0" Height="60" VerticalAlignment="Top" Width="60" HorizontalAlignment="Right" MouseLeftButtonUp="instanceName_MouseLeftButtonUp" Cursor="ScrollNW"
我收到一条错误信息:
The type 'ClassName' cannot have a Name attribute. Value types and types without a default constructor can be used as items within a ResourceDictionary. Line 12 Position 44.
如果我将访问修饰符更改为 public,一切都会按预期工作。
为什么我不能有内部用户控件?
请您参考如下方法:
我认为这是 WPF 的编译模型造成的。我从内存中引用这个并且找不到资源来备份它,但我相信 WPF 编译分两步进行:基本上“代码隐藏”部分首先编译为中间程序集,然后将 XAML 编译为引用该中间体,并将结果组合到最终组装中。
出于这个原因,XAML 引用的类/成员必须公开声明,因为(就编译器而言)它们位于 XAML 的单独程序集中。
编辑: 找到了 Microsoft 引用 here概述了这个过程。特别是,在支持我上面所说的方面,它说:
if one or more XAML files in the project have references to locally defined types, then a temporary .dll file is generated so the final application assemblies may be created after the second pass of markup compilation is complete.
