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

Joh*_*hnB 5 .net wpf xaml visual-studio-2015

为了说明这个问题,我创建了一个简单的示例项目,我在 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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

需要注意的是 FooTestClass 驻留在 Foo 子文件夹中(MVVMTest 文件夹的)

这是我的 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>
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息

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

VS 有问题吗?

mm8*_*mm8 4

如果类型是在与窗口相同的程序集中定义的,则不应指定程序集的名称:

xmlns:mView="clr-namespace:MVVMTest.Foo"
Run Code Online (Sandbox Code Playgroud)

此规则的一个例外是使用XamlReader.Parse方法动态加载某些 XAML 标记。然后,即使类型驻留在同一个程序集中,您也必须指定程序集名称。