【问题标题】:Referencing WPF Control Class in same assembly in Resource Dictionary在资源字典的同一程序集中引用 WPF 控件类
【发布时间】:2016-12-05 15:34:06
【问题描述】:

我有一个扩展 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>

如何让 Mynamespace.CustomControl 在我的 xaml 中工作?

【问题讨论】:

  • 是编译时出错还是设计时出现波浪线?
  • 这是一个设计时曲折的错误。它运行正常,但如果可能的话,我想解决问题的根源,所以我不知道当我遇到错误时会发生什么。

标签: c# wpf xaml controls xml-namespaces


【解决方案1】:

当我在 Visual Studio Community 2015 中按 F7 编译时,它对我有用,在我编译项目之前,第一个自定义控件显示该错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多