wpf XAML 中的命名空间错误中不存在类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14008101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:45:09  来源:igfitidea点击:

Class doesn't exist in namespace error in XAML

c#wpfxamlreferencenamespaces

提问by Jesse

I have class, that selects the ItemDataTemplate for objects. But I can't reference it in my XAML code. (Page.Resources).

我有一个类,它为对象选择 ItemDataTemplate。但我无法在我的 XAML 代码中引用它。(页面。资源)。

It's the Items page in XAML. The class is in the commons folder, and I've referenced the commons folder here:

它是 XAML 中的“项目”页面。该类位于 commons 文件夹中,我在此处引用了 commons 文件夹:

xmlns:common="using:Sample_App.Common"

and then when I wan't to add it to my XAML:

然后当我不想将它添加到我的 XAML 时:

<common:MyDataTemplateSelector x:Key="Selector" AdTemplate="{StaticResource Ad}" NormalTemplate="{StaticResource Normal}"></common:MyDataTemplateSelector>

I get the following error:

我收到以下错误:

The name "MyDataTemplateSelector" does not exist in the namespace "using:MyDataSelector"

命名空间“using:MyDataSelector”中不存在名称“MyDataTemplateSelector”

Here's the MyDataSelector class:

这是 MyDataSelector 类:

namespace MyDataSelector
{
    private class MyDataTemplateSelector : DataTemplateSelector
    {
        public DataTemplate NormalTemplate { get; set; }

        public DataTemplate AdTemplate{ get; set; }

        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {
            if (item is TestApp.Mainpage.NormalData)
                return NormalTemplate
            if (item is TestApp.Mainpage.AdData)
                return AdTemplate;

            return SelectTemplateCore(item, container);
        }
    }
}

回答by ChimeraObscura

You have a couple problems with your code. First of all, you mention that your class is in "the commons folder" - this is totally irrelevant. The location of a code file generally doesn't matter to the compiler, but the namespace you declare the class in doesmatter.

您的代码有几个问题。首先,您提到您的班级在“公共文件夹”中 - 这完全无关紧要。代码文件的位置一般不要紧,编译器,但你在声明类的命名空间的事情。

namespace MyDataSelector // <- This is where your class can be found
{
    private class MyDataTemplateSelector : DataTemplateSelector
    {

So since your class is in the namespace MyDataSelector, the reference in your xaml files should look something like this:

因此,由于您的类位于 namespace 中MyDataSelector,因此您的 xaml 文件中的引用应如下所示:

<Page x:Class="WpfApplication1.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:myDataSelector="using:MyDataSelector">

And you would reference your class like this:

你会像这样引用你的类:

<myDataSelector:MyDataTemplateSelector />

Another issue is that your class is declared as private. That doesn't make sense and probably won't compile. Remove privateto make your class internal, or change it to public.

另一个问题是您的班级被声明为私有。这没有意义,可能无法编译。删除private以使您的类成为内部类,或将其更改为public.