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

问题描述

好了,这真是讨厌,我注意到previously通过WPF加载XAML资源所产生的code似乎没有使用强名称,因此可能是有问题的情况下,您需要支持方通过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.

现在,如果我们看一下code的窗户和用户控件由Visual Studio生成,我们看到了自动生成的文件如下:

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 "..\..\..\Views\ServicePanelUI.xaml"
    System.Windows.Application.LoadComponent(this, resourceLocater);

    #line default
    #line hidden
}

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

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将检查调用程序集的身份和使用它的公钥和版本的详细信息或者查询,其中包含了'这个'参数类型的程序集的身份。

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.

看来这是不是这样的 - 如果你有两个组件具有不同的版本号(但相同的文件名),那么你可以得到一个IOException的消息无法找到资源X(对于上面的例子中无法找到资源意见/ 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'.

更糟的是,我是pretty的肯定,这也将意味着,与文件名相同,但不同的公共/私有密钥,即来自不同出版商组件,也将导致此错误。

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的bug。你不应该使用的AppDomain的隔离,只是为了避免这一点。

推荐答案

您可以在您的项目文件改变的URI在生成的code以下内容:

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?哎呀!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 01:52