XmlnsDefinition似乎不起作用

时间:2013-11-12 15:15:30

标签: .net xaml xml-namespaces

我有一个包含两个项目的解决方案:名为“MyTest.TestApplication”的WPF应用程序和名为“MyTest.ClassLibrary”的类库,以及相应的默认名称空间。 MyTest.TestApplication引用了MyTest.ClassLibrary。 MyTest.ClassLibrary包含一个名为MyTest.ClassLibrary.TestClass的类。我想在应用程序的资源中添加此类的实例,但使用URL作为Xml命名空间值。

所以,我打开MyTest.ClassLibrary.AssemblyInfo.cs,添加一个

using System.Windows.Markup

指令,然后在最后我添加行

[assembly: XmlnsDefinition("http://schemas.test.com/test", "MyTest.ClassLibrary")]

我构建项目,转到MyTest.TestApplication.App.Xaml,然后向Application元素添加名称空间定义。一旦我输入xmlns:t =“,我就会得到一个程序集名称和uris的intellisense列表,但不是http://schemas.test.com/test。如果我仍然添加命名空间定义,就像这样:

xmlns:t="http://schemas.test.com/test"

并将以下内容添加到Resources元素:

<t:TestClass x:Key="myTest" />

然后该资源没有被创建,并且Xaml编辑器抱怨名称空间http://schemas.test.com/test中不存在类TestClass。

令人沮丧的是,我添加了一个名为WpfCustomControlLibrary1的默认WPF自定义控件库,添加了一行

[assembly: XmlnsDefinition("bluh", "WpfCustomControlLibrary1")]

到WpfCustomControlLibrary1.AssemblyInfo.cs,构建了项目,在MyTest.TestApplication中引用它,并且一旦我开始输入xmlns:b =“在App.xam中,intellisense会在”bluh“中显示命名空间列表最顶层。

我尝试添加各种程序集并使用指令到MyTest.ClassLibrary,但我无法通过架构URL让Visual Studio找到我的程序集。令人惊讶的是,如果我将命名空间设置为程序集名称,它确实有效,如下所示:

xmlns:t="clr-namespace:MyTest.ClassLibrary;assembly=MyTest.ClassLibrary"

那么......这里发生了什么?

编辑:令人沮丧的是,经过两个小时的搜索后,我终于通过从WPF应用程序中删除引用然后重新添加它来使其工作。啊。我想这是某种VS2012错误?

2 个答案:

答案 0 :(得分:1)

我认为&#34; ClassLibrary&#34;存在问题。作为程序集名称。刚刚花了一些时间,因为Resharper回来了

System.TypeLoadException : Could not load type 'BMN.Utils.Logging.ConfigLogger' 
from assembly 'ClassLibrary, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.

将程序集名称更改为BMN.Utils.Logging.UnitTests解决了这个问题。

答案 1 :(得分:0)

由于“bluh”有效,请尝试:

[assembly: XmlnsDefinition(@"http://schemas.test.com/test", "MyTest.ClassLibrary")]

也许斜线会抛弃编译器。