错误 "标记在XML命名空间中不存在"

5
为了说明问题,我创建了一个简单的示例项目,在 XAML 定义中遇到了编译错误。
以下是我的类定义:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MVVMTest.Foo
{
    public class FooTestClass
    {
        public String Name { get; set; }

        public String Key { get; set; }
    }
}

需要注意的是FooTestClass位于MVVMTest文件夹的Foo子文件夹中。

以下是我的XAML代码:

<Window x:Class="MVVMTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MVVMTest"
        xmlns:mView="clr-namespace:MVVMTest.Foo;assembly=MVVMTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <mView:FooTestClass x:Key="ddd" />           
    </Window.Resources>
    <Grid>
    </Grid>
</Window>

我遇到了以下错误信息:

标记“FooTestClass”在XML命名空间“clr-namespace:MVVMTest.Foo;assembly=MVVMTest”中不存在。行12,位置10。
MVVMTest C:\Dev Projects\MVVMTest\MVVMTest\MainWindow.xaml

这是由于VS出现了问题吗?

这个SO有一个答案建议删除程序集的值并将其留空。 " xmlns:ZZZ="clr-namespace:YYY;assembly=" ". - Ethilium
SO - 谢谢,就这样吧。我在XAML的命名空间方面仍然看到相当不稳定的行为,但现在似乎可以工作了。 - JohnB
2个回答

4
我发现了另一个可能导致此错误的原因,所以我将在这里留下它供未来读者参考。如果命名空间解决方案无法解决问题,请确保您的项目和DLL都针对相同的框架版本进行定位。方法是:单击“项目”菜单 -> “属性”。如下图所示:enter image description here

4

如果类型在同一个程序集中定义,您不应该指定程序集的名称:

xmlns:mView="clr-namespace:MVVMTest.Foo"

这个规则有一个例外,就是当你使用XamlReader.Parse方法动态加载一些XAML标记时。此时,即使类型位于同一程序集中,你也必须指定程序集名称。

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