c# - 为命名空间指定程序集

标签 c# wpf xaml namespaces .net-assembly

在 C# 中是否可以指定程序集和命名空间?

例如,如果您在项目中同时引用 PresentationFramework.AeroPresentationFramework.Luna,您可能会注意到它们在同一命名空间中共享相同的控件,但是具有不同的实现方式。

ButtonChrome为例。它存在于命名空间 Microsoft.Windows.Themes 下的两个程序集中。

在 XAML 中,您将程序集与命名空间一起包括在内,所以这里没有问题

xmlns:aeroTheme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:lunaTheme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"

<aeroTheme:ButtonChrome ../>
<lunaTheme:ButtonChrome ../>

但是在后面的 C# 代码中,无论如何我都找不到在 PresentationFramework.Aero 中创建 ButtonChrome 的实例。

下面的代码在编译时给我error CS0433

using Microsoft.Windows.Themes;
// ...
ButtonChrome buttonChrome = new ButtonChrome();

error CS0433: The type 'Microsoft.Windows.Themes.ButtonChrome' exists in both
'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\Profile\Client\PresentationFramework.Aero.dll'
and
'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\Profile\Client\PresentationFramework.Luna.dll'

这很好理解,编译器无法知道选择哪个 ButtonChrome 因为我没有告诉它。我能以某种方式做到这一点吗?

最佳答案

您需要为程序集引用指定别名,然后通过别名导入:

extern alias thealias;

请参阅属性窗口以获取引用。

假设您将 aero 组件别名为“aero”,将 luna 组件别名为“luna”。然后,您可以在同一个文件中使用这两种类型,如下所示:

extern alias aero;
extern alias luna;

using lunatheme=luna::Microsoft.Windows.Themes;
using aerotheme=aero::Microsoft.Windows.Themes;

...

var lunaButtonChrome = new lunatheme.ButtonChrome();
var aeroButtonChrome = new aerotheme.ButtonChrome();

参见 extern alias了解更多信息。

关于c# - 为命名空间指定程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10956326/

相关文章:

c# - visual studio 2010 F10/F11 不会中断类成员初始化代码

c# - 短+短!=短?

c# - 覆盖 C# 中的 HttpContext.Current.Request.Url 属性以返回编码的 URL

c# - 如何在 C# 中将来自网络摄像头的视频编码为 H.264?

wpf - 声明式进度条绑定(bind)

c# - 如何在 C# 中按类型查找 .cs 文件的路径

wpf - 调整单选按钮的大小

.net - 是否有人对WPF在64位操作系统上的性能有任何统计或想法?

xaml - VS 2017 NetStandard XAML 智能感知

wpf - 在 XAML 中设置 Grid 或 StackPanel 元素之间的距离的最佳方法是什么?