简体   繁体   English

WPF:从不是正在执行的程序集的程序集(dll)加载ImageSource

[英]WPF: Loading ImageSource from an assembly (dll) which is not the executing assembly

I have a question regarding Pack Uri. 我有一个关于Pack Uri的问题。

I created a WPF .NET 4.5 application. 我创建了WPF .NET 4.5应用程序。 I would like to make it quite modular to be open for 3rd party plugins; 我想使其模块化,以便为第三方插件开放; so i split it up in three projects. 所以我将其分为三个项目。

My Configurator executable will be the main entrance point. 我的Configurator可执行文件将是主要入口。 This configurator assembly references a Types.dll, which contains most of my base logic. 此配置程序程序集引用Types.dll,其中包含我的大部分基本逻辑。 For my guided user interface, I use a third project, called UserInterface.dll 对于指导的用户界面,我使用第三个项目,称为UserInterface.dll。

Now my types.dll contains an image file, which I would like to use as resource in my UserInterface.dll. 现在,我的types.dll包含一个图像文件,我想将其用作UserInterface.dll中的资源。

If I try to call it eg using: 如果我尝试调用它,例如使用:

   Image img = new Image();
   img.Source = new BitmapImage(new Uri("/Types;component/Resources/myPicture.png", UriKind.Relative);

The image will not show up, because it cannot not found. 该图像将不会显示,因为找不到。 My executing assembly is UserInterface.dll, but Types.dll is referenced in Visual Studio. 我正在执行的程序集是UserInterface.dll,但是在Visual Studio中引用了Types.dll。 Same effect using absolute Uris (application,,,:). 使用绝对Uris(应用程序、、、 :)具有相同的效果。

I did a work around defining my image as Resource (Project Property) and load the Drawing.Bitmap, convert it to an ImageSource and assigning it to my image. 我做了一些工作,将图像定义为Resource(项目属性),然后加载Drawing.Bitmap,将其转换为ImageSource并将其分配给我的图像。 But I think it is not as performant as loading it directly via ImageSource. 但是我认为它的性能不如直接通过ImageSource加载它。

So my question is: How can I load a resource in a called assembly from another assembly, when both are not the main application? 所以我的问题是:当两者都不都是主应用程序时,如何从另一个程序集中从一个被调用的程序集中加载资源?

Thanks for your help. 谢谢你的帮助。

EDIT: 编辑:

The images's build action is set to Resource. 图像的构建操作设置为“资源”。

Use more verbose definition of Pack URI for referenced assembly. 使用Pack URI的更详细的定义来引用程序集。 Refer to Referenced Assembly Resource File section under Pack URI in WPF . 请参考WPF中Pack URI下的“引用的程序集资源文件”部分。

It will look like this: 它看起来像这样:

img.Source = new BitmapImage(new Uri("pack://application:,,,/Types;component/Resources/myPicture.png");

Also make sure Build Action for myPicture.png is set to Resource . 还要确保将myPicture.png的Build Action设置为Resource

Short Answer: 简短答案:

The answer may be, that there are some issues with .NET 4.5.1 or the error was somewhere else. 答案可能是.NET 4.5.1出现问题,或者该错误在其他地方。 Switching back to .NET 4.5 worked for me. 切换回.NET 4.5对我有用。 Referencing a resource out of a referenced dll works using uri, too. 从引用的dll中引用资源也可以使用uri。 If I find out, what I did wrong I'll update this. 如果发现错误,我将进行更新。

Coming to the "solution": 来到“解决方案”:

Thank's a lot for your help, Rohit, the application works now. 非常感谢您的帮助,Rohit,该应用程序现在可以运行了。 I don't know how I did it, but I did it :) Thank you for taking the time making a sample solution, should have done this before posting here... 我不知道我是怎么做到的,但是我做到了:)感谢您抽出宝贵的时间制作示例解决方案,应该在发布之前完成此操作...

I wonder where the problem came from. 我想知道问题出在哪里。 I built a small sample, as you suggested and got the same results as you did. 根据您的建议,我构建了一个小样本,并获得了与您相同的结果。 I switched back to my application to see what I did different there. 我切换回我的应用程序以查看我在那里所做的不同。 First I checked all my References on the WPF assemblies. 首先,我检查了WPF程序集上的所有参考。 They were all like in my new sample project. 它们都像在我的新样本项目中一样。 I switched my application back to .NET 4.5 (from .NET 4.5.1) and then it worked in my app, too. 我将应用程序切换回.NET 4.5(从.NET 4.5.1),然后它也可以在我的应用程序中工作。 After that I switched back to .NET 4.5.1 to see, if I get the error again - but it still works. 之后,我切换回.NET 4.5.1以查看是否再次收到错误-但它仍然有效。 It even could not be a typo, as I am using the same code (commented it out) both times. 甚至不能打错字,因为我两次都使用相同的代码(注释掉了)。 So what can it be, that caused this problem? 那到底是什么引起了这个问题呢? Perhaps you have an idea? 也许您有一个主意?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM