包URI中引用的程序集资源 [英] Pack URI to resources in referenced assembly

查看:150
本文介绍了包URI中引用的程序集资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在敲打我的头在这个有一段时间了,但对上帝的爱,我只是找不出什么毛病我的URI。也许有人可以帮助。

I've been banging my head over this for some time, but for the love of god, I just cant figure out whats wrong with my uri. Maybe someone could help.

我正在开发的第三方软件的插件(意味着我必须App.config中没有访问和修改着应用程序本身)。该插件位于从exe文件的位置不同,一个文件夹中。我有位于MyAddin.View.dll WPF窗口。最近我已经决定,将所有WPF资源在单独的程序(称为UI.Shared)。我已经添加UI.Shared.dll为MyAddin.View.dll参考,我还修改MyAddin.View.dll窗口内我包URI这样:

I'm developing an addin for a third-party software (means I have no access to App.config and cant modify application itself). The addin is located in a folder that differs from location of the exe file. I have a wpf window located in MyAddin.View.dll. Recently I've decided to move all WPF resources in a separate assembly (called UI.Shared). I've added UI.Shared.dll as a reference to MyAddin.View.dll, I've also modified my pack uri inside MyAddin.View.dll window to this:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary
         Source="pack://application:,,,/UI.Shared;component/Styles/Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</Window.Resources>



我确信,Style.xaml生成操作设置为资源。该UI.Shared.dll位于同一文件夹MyAddin.View.dll(但两者都是在同一个文件夹中的应用程序的可执行文件)。一切正常,在设计时的罚款。但在运行时,我得到:搜索结果
设置属性System.Windows.ResourceDictionary.Source引发了异常。搜索结果
和内部异常说:搜索结果
无法加载文件或程序集UI.Shared,文化=中性或之一它的依赖。该系统找不到指定的文件。搜索结果

I've made sure that Style.xaml Build Action is set to Resource. The UI.Shared.dll is located in the same folder as MyAddin.View.dll (but both of them are NOT in the same folder as application executable). Everything works fine at design time. But during run-time, I get:

"Set property 'System.Windows.ResourceDictionary.Source' threw an exception."

And the inner exception says:

Could not load file or assembly 'UI.Shared, Culture=neutral' or one of its dependencies. The system cannot find the file specified.

在我搬到资源到一个单独的一切工作就好了装配:(。可能有人请帮忙吗?

Everything worked just fine before I've moved resources into a separate assembly:(. Could someone please help?

推荐答案

您URI是好的。

我从VBA调用WPF窗口时也有类似的问题:WPF无法找到引用的资源,由于主要过程是从一个不同的目录开始我找到了解决办法可能是你的情况也是有用的。

I've had a similar problem when calling WPF windows from VBA: WPF was unable to find the referenced resources, since the main process was started from a different directory. The solution I found might be useful for your case as well:

下面是一些(未经测试)C#示例代码,通过一些VB的启发我们已经在生产中使用.NET代码:

Here is some (untested) C# example code, inspired by some VB.NET code that we have in production use:

// Do this when your add-in starts
var addinAssembly = Assembly.GetExecutingAssembly();

AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
{
    var missing = new AssemblyName(e.Name);

    // Sometimes the WPF assembly resolver cannot even find the executing assembly...
    if (missing.FullName == addinAssembly.FullName)
        return addinAssembly;

    var addinFolder = Path.GetDirectoryName(addinAssembly.Location);
    var missingPath = Path.Combine(addinFolder, missing.Name + ".dll");

    // If we find the DLL in the add-in folder, load and return it.
    if (File.Exists(missingPath))
        return Assembly.LoadFrom(missingPath);

    // nothing found
    return null;
};

这篇关于包URI中引用的程序集资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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