一段时间以来,我一直在为此苦思冥想,但看在上帝的份上,我就是想不通我的 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 无法找到引用的资源,因为主进程是从不同的目录启动的。我找到的解决方案也可能对您的情况有用:
- 您将事件处理程序附加到
AppDomain.AssemblyResolve
event .如果未找到程序集,则在加载项目录中搜索它。
这是一些(未经测试的)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/