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

大兔子大兔子 提交于 2021-02-05 11:31:07

问题


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");


回答1:


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.



来源:https://stackoverflow.com/questions/60433334/how-can-i-reference-a-resource-png-from-a-seperate-project-in-the-same-solutio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!