如何从同一解决方案中的单独项目引用资源 (png) [英] How can I reference a resource (png) from a seperate project in the same solution

查看:32
本文介绍了如何从同一解决方案中的单独项目引用资源 (png)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案结构如下:

My solution is structured as follows:

  1. 类库(名称 = Chess)
    • 图片
      • Pawn.png
  • 主窗口.xaml

我想在图像标记的 MainWindow.xaml 内部显示 Pawn.png 图像.当我的 WPF 项目中有图像文件时,我可以通过相对路径轻松引用它们.当资源位于不同的项目中时,我无法解决此问题.

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.

我在 Image 文件夹中的图像具有构建操作 Resource 并且始终复制到输出目录.Chess 项目在 ChessApp 中被正确引用.

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:如何使用来自另一个的资源WPF 中的项目?

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


没有 URI:我如何使用来自我 XAML 中另一个项目的图像?

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


所以我不知道该怎么办.


So I don't know what to do.

解决方法:
向一个类添加一个属性,该类从相对路径返回图像的完整路径 Path.GetFullPath(@"..\..\..\..\Chess\Images\Pawn.png");

推荐答案

用一个普通的 netcoreapp3.1 类库尝试了这个.

Tried this with a plain netcoreapp3.1 class library.

虽然在 XAML 设计器中,ChessApp 的 MainWindow 正确显示了图像资源,但我在运行时看到了 System.Resources.MissingManifestResourceException.

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

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

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

解决了问题.

这篇关于如何从同一解决方案中的单独项目引用资源 (png)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆