c# - 将 URI 打包到引用程序集中的资源

标签 c# wpf xaml

一段时间以来,我一直在为此苦思冥想,但看在上帝的份上,我就是想不通我的 uri 出了什么问题。也许有人可以提供帮助。

我正在为第三方软件开发插件(这意味着我无权访问 App.config 并且无法修改应用程序本身)。插件位于不同于 exe 文件位置的文件夹中。我在 MyAddin.View.dll 中有一个 wpf 窗口。最近我决定将所有 WPF 资源移动到一个单独的程序集(称为 UI.Shared)中。我添加了 UI.Shared.dll 作为对 MyAddin.View.dll 的引用,我还在 MyAddin.View.dll 窗口中修改了我的包 uri:

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

</Window.Resources>

我已确保将 Style.xaml Build Action 设置为 Resource。 UI.Shared.dll 与 MyAddin.View.dll 位于同一文件夹中(但它们都与应用程序可执行文件位于同一文件夹中)。在设计时一切正常。但是在运行时,我得到:

“设置属性‘System.Windows.ResourceDictionary.Source’引发异常。”

内部异常说:

无法加载文件或程序集“UI.Shared,Culture=neutral”或其依赖项之一。系统找不到指定的文件。

在我将资源移动到一个单独的程序集之前,一切正常:(。有人可以帮忙吗?

最佳答案

您的 URI 没问题。

从 VBA 调用 WPF 窗口时,我遇到了类似的问题:WPF 无法找到引用的资源,因为主进程是从不同的目录启动的。我找到的解决方案也可能对您的情况有用:

这是一些(未经测试的)C# 示例代码,灵感来 self 们在生产中使用的一些 VB.NET 代码:

// 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;
};

关于c# - 将 URI 打包到引用程序集中的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20495778/

相关文章:

wpf - 如何在动态资源中更新动态资源?

c# - Visual Studio 附加到进程并在中断时查看代码?

c# - 为什么var不能应用于成员变量

c# - weka .net ikvm -> 为什么加载 csv 文件这么慢?

wpf - 在内容中制作带有矢量图形的按钮

c# - Prism 。关闭使用 IDialogService 创建的对话框

c# - 将参数传递给c#中的sql存储过程

c# - 为遗留 WinForms MDI 应用程序设计新的 UI

c# - 如何在Windows Phone 8应用程序的应用程序栏中隐藏/显示图标按钮?

wpf - 标签不是有效的 XAML 元素?