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

时间:2017-02-01 20:33:27

标签: .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; }
    }
}

应该注意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>

我收到以下错误消息

  

XML名称空间中不存在标记'FooTestClass'   'CLR-名称空间:MVVMTest.Foo;装配= MVVMTest'。 12号线位置10.
  MVVMTest C:\ Dev Projects \ MVVMTest \ MVVMTest \ MainWindow.xaml

VS中是否存在问题?

2 个答案:

答案 0 :(得分:2)

我发现了可能发生此错误的另一个原因,所以我会留待未来的读者使用。

如果命名空间解决方案不起作用,请确保您的项目和DLL都针对相同的框架版本。

菜单Project - &gt; Properties

enter image description here

答案 1 :(得分:0)

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

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

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

相关问题