我有一个继承自System.Windows.Controls.Control的类,我打算将其转换为自定义控件以在我的WPF应用程序中使用,如下所示:
namespace MyNamespace
{
public class CustomControl : Control
{
// Implementation
}
}
所有的Windows和代码文件都使用MyNamespace命名空间,无论是XAML文件还是.CS文件。因此,在同一命名空间(MyNamespace)中引用该类应该很容易,方法如下:
<Window x:Class="MyNamespace.CustomWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlsn:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace"
Title="MyCustomWindow">
<Grid>
<local:CustomControl />
</Grid>
</Window>
但是当我这样做时,会出现以下错误信息:
The name "CustomControl" does not exist in the namespace "clr-namespace:MyNamespace"
。然而,如果我在另一个程序集中定义了另一个自定义控件,我可以像这样成功添加它:<Window x:Class="MyNamespace.CustomWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlsn:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace"
xmlns:otherAssembly="clr-namespace:OtherNamespace;assembly=OtherAssembly"
Title="MyCustomWindow">
<Grid>
<otherAssembly:OtherCustomControl />
</Grid>
</Window>
我该如何在xaml中让Mynamespace.CustomControl起作用?