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

一段时间以来,我一直在为此而奋斗,但为了上帝的爱,我无法弄清楚我的uri是什么问题。 也许有人可以帮忙。

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

       

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

“Set属性’System.Windows.ResourceDictionary.Source’引发了exception。”

内在的例外说:

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

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

你的URI很好。

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

  • 您将事件处理程序附加到AppDomain.AssemblyResolve事件 。 如果未找到程序集,则在外接程序目录中搜索它。

这是一些(未经测试的)C#示例代码,受到我们在生产中使用的一些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; };