2017-02-01 31 views
1

为了说明这个问题,我已经创建了我在哪里上的XAML定义得到编译错误一个简单的示例项目。错误“标签并没有在XML命名空间存在”

这里是我的类定义:

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文件夹)

这是我的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:\开发项目\ MVVMTest \ MVVMTest \ MainWindow.xaml

有没有在VS的问题吗?

+0

此[SO](http://stackoverflow.com/questions/6794482/tag-does-not-exist-in-xml-namespace)有一个答案提示移除所述组件值和离开它是空的。 “xmlns:ZZZ =”clr-namespace:YYY; assembly =“”。 – Ethilium

+0

所以 - 谢谢,就是这样。我仍然在使用XAML中的名称空间看到漂亮的行为,但它现在似乎工作。 – JohnB

回答

0

,如果该类型在同一个组件设置为窗口定义应该指定程序集的名称:

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

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

相关问题