如何强制 WPF 使用使用程序集强名称的资源 URI?啊! [英] How to force WPF to use resource URIs that use assembly strong name? Argh!

查看:26
本文介绍了如何强制 WPF 使用使用程序集强名称的资源 URI?啊!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这真的很烦人,我之前注意到 WPF 生成的用于加载 XAML 资源的代码似乎没有使用强名称,因此在需要支持并排版本的 WPF 程序集的情况下可能会出现问题.

O.k, this is really irritating, I had noticed previously that the code generated by WPF for loading XAML resources did not appear to use strong names and therefore may be problematic for scenarios where you need to support side by side versions of WPF assemblies.

事实证明确实如此,它现在给我带来了问题 - 我有一个插件系统,它应该支持并排安装插件,这些插件仅在版本号(它们的程序集版本)上有所不同.这当然可以由 .NET 支持,因为即使程序集具有相同的 DLL 文件名,它们也被确定为具有不同的身份,前提是它们具有强命名并且具有不同的公钥/私钥或具有不同的程序集版本号.

This has turned out to be the case, and it's now causing me problems - I have a plug-in system which is supposed to support side by side installation of plugins which differ only in their version numbers (their assembly versions). This of course can be supported by .NET since assemblies are determined to have different identities even if they have the same DLL filename, provided that they are strongly named and either have a different public/private key OR have a different assembly version number.

现在,如果我们查看 Visual Studio 为 windows 和用户控件生成的代码,我们会在自动生成的文件中看到以下内容:

Now, if we look at the code generated for windows and usercontrols by visual studio, we see in the auto-generated file the following:

/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {
    if (_contentLoaded) {
        return;
    }
    _contentLoaded = true;
    System.Uri resourceLocater = new System.Uri("/Sensormatic.AMK1000.Panel;component/views/servicepanelui.xaml", System.UriKind.Relative);

    #line 1 "......ViewsServicePanelUI.xaml"
    System.Windows.Application.LoadComponent(this, resourceLocater);

    #line default
    #line hidden
}

请注意创建资源定位器的行 - 它使用的是相对 URI,该 URI 未指定强名称或包含 xaml 资源的程序集版本.

Notice the line where the resource locater is created - it is using a relative URI which does not specify the strong name or the version of the assembly which contains the xaml resource.

我认为 LoadComponent 可能会检查调用程序集的身份并使用它的公钥和版本详细信息,或者可能检查包含this"参数类型的程序集的身份.

I thought maybe LoadComponent would check the calling assembly's identity and use it's public key and version details or perhaps check the identity of the assembly which contains the type for the 'this' parameter.

事实并非如此 - 如果您有两个具有不同版本号(但文件名相同)的程序集,那么您会收到带有消息无法定位资源 X"的 IOException(例如无法定位资源")视图/servicepanelui.xaml'.

It appears this is not the case - if you have two assemblies with different version numbers (but the same filename) then you can get an IOException with the message "Cannot locate resource X" (for above example "Cannot locate resource 'views/servicepanelui.xaml'.

更糟糕的是,我很确定这也意味着具有相同文件名但不同公钥/私钥(即来自不同发布者)的程序集也会导致此错误.

Worse, I'm pretty sure that this is also going to mean that assemblies with the same filename but different public/private key, i.e. from different publishers, will also result in this error.

那么,有人知道如何解决这个问题吗?如何使 WPF 强名称兼容.

请注意,就我而言,这是一个 WPF 错误.您不应该为了避免这种情况而使用 Appdomain 隔离.

推荐答案

您可以在项目文件中设置以下内容以更改生成代码中的 URI:

You can set the following in your project file to change the URI's in the generated code:

<PropertyGroup>
  <AssemblyVersion>1.0.0.0</AssemblyVersion>
  <AssemblyPublicKeyToken>[YOUR_PUBLIC_KEY_TOKEN]</AssemblyPublicKeyToken>
</PropertyGroup>

这篇关于如何强制 WPF 使用使用程序集强名称的资源 URI?啊!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆