简体   繁体   中英

Visual Studio: What's the correct way to reference resources across projects?

An .xaml image location in a secondary reference is not becoming the correct resource location during runtime, it refers to the primary reference resources folder instead. How can I change this?

Background -

I have a solution with several projects. The important three projects here are

  1. a WPF project which builds as a class library
  2. a default class library
  3. a console app for testing

The WPF project has a .png image in its resources. In the MainWindow created it's called via:

<ImageBrush
  ImageSource="pack://siteoforigin:,,,/Resources/MyDemoImage.png" 
  Stretch="Uniform"/>

When I run an instance of the WPF window directly through the console app , it works fine. The resource reference becomes during runtime:

C:\\Users\\me.me\\Documents\\Visual Studio 2015\\Projects\\sol\\ WPFProjectName\\ bin\\Resources\\MyDemoImage.png

But if I create a reference to the WPF project within the class library project then call an instance of the class library, the address becomes

C:\\Users\\me.me\\Documents\\Visual Studio 2015\\Projects\\sol\\ ClassLibraryName\\ bin\\Resources\\MyDemoImage.png

So this "pack" address becomes a reference to the class library resources rather than the WPF project resources folder.

Now, I think I could simply add the resource to that middle-man class library too, but I'm wondering if there's some correct way to reference a "secondary" resource, so I only have to add images once?

By the way, the error I get when I try to compile this is System.Windows.Baml2006.TypeConverterMarkup - I've found answers regarding this but tried what they say to no avail (mark image as copy always etc.)

Change

pack://siteoforigin:,,,/Resources/MyDemoImage.png

to

pack://application:,,,/AssemblyName;component/Resources/MyDemoImage.png

Thanks to @Clemens for his advice. This worked great.

I just wanted to answer and share a bit of other frustration-gathered knowledge I found while experimenting with this.

Using pack://application:,,,/AssemblyName;component/Resources/MyDemoImage.png worked great for me for my first image. I then had loads of trouble trying to do the same thing for a 2nd/ 3rd image. It kept compiling ok but on runtime the inner exceptions were throwing out that it couldn't find "resources/mydemoimage.png"

The reason for this was due to the BuildAction on the images. I'd been playing with the different types, some of which worked for my local copy (eg setting BuildAction to Embedded Resource actually fixed the initial issue I was having) BUT for the pack://app... to work, compile and then carry through to install and be instanced in another application (eg VBA in Excel)...

The BuildAction MUST be Resource

I was extremely lucky that I'd set this by chance before trying Clemens' answer because if I hadn't I would have assumed it just didn't work! But it does!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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