dodo978
dodo978

Reputation: 27

How can I reference a resource (png) from a seperate project in the same solution

My solution is structured as follows:

  1. Class Library (Name = Chess)
    • Images
      • Pawn.png
  2. WPF Core 3.1 Project (Name = ChessApp)
    • MainWindow.xaml

I want to show the Pawn.png image inside my MainWindow.xaml inide of an image tag. When I have image files inside my WPF project, I can easily reference them by relative path. When the resources are in a different project, I am not able to fix this.

My images inside the Image folder a have the build action Resource and are always copied to the output directory. The project Chess is correctly referenced inside ChessApp.

Obviously there are a lot of similar questions, but none of the answers worked for me. All I have tried:

URI: How to use Resources of from another project in WPF?

<Image Source="pack://application:,,,/Chess;Component/Images/Pawn.png"></Image>


Without URI: How do I use an image from another project in my XAML?

<Image Source="/Chess;Component/Images/Pawn.png"></Image>


So I don't know what to do.

Workaround:
Add a property to a class which returns the full path of the image out of the relative one Path.GetFullPath(@"..\..\..\..\Chess\Images\Pawn.png");

Upvotes: 0

Views: 392

Answers (1)

Clemens
Clemens

Reputation: 128061

Tried this with a plain netcoreapp3.1 class library.

While in XAML Designer the ChessApp's MainWindow correctly shows the image resource, I see a System.Resources.MissingManifestResourceException at runtime.

Changing the class library project's Sdk property from

<Project Sdk="Microsoft.NET.Sdk">

to

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

fixed the problem.

Upvotes: 1

Related Questions