首页 \ 问答 \ 如何从XAML中的CLR命名空间映射类型而不是声明它的汇编?(How do I map a type from a CLR namespace in XAML from an assembly other than the one where it's declared?)

如何从XAML中的CLR命名空间映射类型而不是声明它的汇编?(How do I map a type from a CLR namespace in XAML from an assembly other than the one where it's declared?)

在XAML中,我想使用来自两个不同程序集的类型,每个程序集都有自己的命名空间。 我不想在xmlns:<xml-namespace>="<clr-namespace>"属性中显式声明名称空间,而是使用[XmlnsDefinition]程序集属性将URI映射到这些类型的名称空间。

其中一个程序集本身与WPF无关,所以我想避免它引用与WPF相关的程序集,特别是System.Xaml.dll程序集,如果该程序集使用[XmlnsDefinition]属性。

我有一个像这样组织的Visual Studio解决方案:

Gu.Units.sln
    Gu.Units.csproj // no ref to System.Xaml here
    Gu.Units.Wpf.csproj // references Gu.Units and System.Xaml

Gu.Units.Wpf.csproj我有这个映射:

[assembly: XmlnsDefinition("http://Gu.com/Units", clrNamespace: "Gu.Units", AssemblyName = "Gu.Units")]
[assembly: XmlnsDefinition("http://Gu.com/Units", clrNamespace: "Gu.Units.Wpf", AssemblyName = "Gu.Units.Wpf")]
[assembly: XmlnsPrefix("http://Gu.com/Units", "units")]

我试图在XAML中使用它,如下所示:

<UserControl x:Class="Gu.Units.Wpf.Demo.Sample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:units="http://Gu.com/Units">
    <Label Content="{x:Static units:LengthUnit.Millimetres}" />
</UserControl>

但由于某种原因,名称空间Gu.Units似乎被忽略了。 也就是说,它不包含在URI http://Gu.com/Units标识的XML命名空间中。 相反,我得到:

名称“LengthUnit”在命名空间“ http://Gu.com/Units ”中不存在。

XAML中的显式名称空间声明 - 即具有xmlns:units="clr-namespace:Gu.Units;assembly=Gu.Units" - 工作正常,但我也想避免这种情况。

有没有办法,我可以让我的Gu.Units.Wpf.dll程序集提供必要的[XmlnsDefinition]属性来映射Gu.Units.Wpf.dll程序[XmlnsDefinition]的命名空间,以便后者本身不需要对System.Xaml.dll的引用System.Xaml.dllSystem.Xaml.dll没有任何特定于XAML的代码?


In XAML, I would like to use types from two different assemblies, each with their own namespace. Rather than declaring the namespaces explicitly in an xmlns:<xml-namespace>="<clr-namespace>" attribute, I would like to use the [XmlnsDefinition] assembly attribute to map a URI to the namespaces for those types.

One of the assemblies is not relevant to WPF per se, so I would like to avoid it having any reference to WPF-related assemblies, and in particular the System.Xaml.dll assembly which would be required if that assembly used the [XmlnsDefinition] attribute.

I have a Visual Studio Solution that is organized like this:

Gu.Units.sln
    Gu.Units.csproj // no ref to System.Xaml here
    Gu.Units.Wpf.csproj // references Gu.Units and System.Xaml

In Gu.Units.Wpf.csproj I have this mapping:

[assembly: XmlnsDefinition("http://Gu.com/Units", clrNamespace: "Gu.Units", AssemblyName = "Gu.Units")]
[assembly: XmlnsDefinition("http://Gu.com/Units", clrNamespace: "Gu.Units.Wpf", AssemblyName = "Gu.Units.Wpf")]
[assembly: XmlnsPrefix("http://Gu.com/Units", "units")]

I have tried to use it in XAML like this:

<UserControl x:Class="Gu.Units.Wpf.Demo.Sample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:units="http://Gu.com/Units">
    <Label Content="{x:Static units:LengthUnit.Millimetres}" />
</UserControl>

But for some reason, the namespace Gu.Units seems to be ignored. That is, it is not included in the XML namespace identified by the URI http://Gu.com/Units. Instead, I get:

The name "LengthUnit" does not exist in the namespace "http://Gu.com/Units".

An explicit namespace declaration in the XAML — i.e. having xmlns:units="clr-namespace:Gu.Units;assembly=Gu.Units" — works fine but I would also like to avoid that.

Is there a way that I can have my Gu.Units.Wpf.dll assembly provide the necessary [XmlnsDefinition] attribute to map the namespace from the Gu.Units.dll assembly, so that the latter does not itself need a reference to System.Xaml.dll, nor have any XAML-specific code in it at all?


原文:https://stackoverflow.com/questions/33862121
更新时间:2020-10-12 19:10

最满意答案

如果我理解你正确,使用你的语法,你想要掌握你的PhoneListController对象,这样你就可以定义它的解析属性,对吧?

你可以这样做:

var phoneListCtrl = mod.controller('PhoneListController', ["$scope", function($scope) {
    foo
    bar
    …
}]);

phoneListCtrl.resolve = {

    //code here

}

If I understood you right, using your syntax, you want to get hold of your PhoneListController object so you can define It's resolve property, right?

You can do this:

var phoneListCtrl = mod.controller('PhoneListController', ["$scope", function($scope) {
    foo
    bar
    …
}]);

phoneListCtrl.resolve = {

    //code here

}

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。