在资源字典中引用同一程序集中的 WPF 控件类

3

我有一个继承自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起作用?


是编译期间的错误还是设计时的波浪线? - Cool Blue
这是一个设计时的波浪线错误。虽然它可以正常运行,但如果可能的话,我想解决问题的根源,这样当出现错误时就不会感到困惑了。 - user3685285
1个回答

0

我在使用Visual Studio Community 2015编译之前,按下F7进行编译后,它对我起作用了。在编译项目之前,第一个自定义控件显示了该错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接