2013-12-10 52 views
4

我一直在嘲笑我一段时间,但对于上帝的爱,我无法弄清楚我的uri有什么问题。也许有人可以帮忙。在引用的程序集中将资源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生成操作被设置为资源。 UI.Shared.dll位于与MyAddin.View.dll相同的文件夹中(但它们都是不是与应用程序可执行文件位于同一文件夹中)。在设计时一切正常。但在运行时,我得到:

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

和内异常说:

无法加载文件或程序集“UI.Shared文化=中性”或它的一个依赖。该系统找不到指定的文件。

一切工作只是罚款之前,我搬到资源投入到一个单独的程序:(。可能有人请帮忙吗?

回答

2

你的URI是好的。

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

下面是一些(未经测试)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; 
}; 
+0

嗯,这个工作,但我希望会有一个更清洁解。如果不是,我会将其标记为答案。 –

+1

这是关于我见过的AssemblyResolve的最干净的实现。 :) –