无法使用XAML中的相对路径引用图像源 [英] Not able to reference Image source with relative path in xaml

查看:88
本文介绍了无法使用XAML中的相对路径引用图像源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个ClassLibrary项目,并添加了xaml的Window类型.我编写了一个控制台应用程序,并显示了该wpf窗口.

I have created a ClassLibrary project, and added a xaml of Window type. I wrote a console application and showing this wpf window.

问题是我必须在此窗口中显示一个图标.

The problem is I have to show an Icon in this window.

如果我使用以下代码,则无法正常工作

If I am using following code it is not working

<Image Source="../Images/Folder-icon.png"></Image>

当我给出一些硬编码的路径时

When I give some hard coded path like

<Image Source="E:\MyApp\Images\Folder-icon.png"></Image>

有效.

为什么我必须给出一个硬编码的路径值?有什么解决办法吗?

Why I have to give an hard coded path value? Is there any solution?

希望我提供了足够的信息.请帮我解决这个问题超过2个小时.

Hope I gave enough information. Please help I am struggling with this problem for more that 2 hours.

推荐答案

如果在使用该项目的同一项目中添加了Images文件夹,则此方法应该对您有用:

If Images folder is added in same project of its usage, this should work for you:

<Image Source="/Images/Folder-icon.png"/>


或使用 打包Uri 专门:

<Image Source="pack://application:,,,/Images/Folder-icon.png"/>


如果图像与XAML所在的当前项目不在同一项目中,则必须使用Pack URI的更详细定义,在其中必须指定添加图像的程序集名称.


In case image resides in different project than current project where your XAML resides, you have to use more verbose definition of Pack URI where you have to specify the assembly name where image is added.

<Image Source="pack://application:,,,/Assembly;component/Images/Folder-icon.png"/>

用添加图像的实际程序集名称替换程序集.

Replace Assembly with actual assembly name where image is added into.

还要确保将文件 Folder-icon.png的 Build Action 设置为 Resource .

更新:

Pack URI的详细定义也适用于示例.试试看:

More verbose definition of Pack URI works in sample as well. Try this out:

<Image Source="pack://application:,,,/ClassLibrary1;component/Folder-icon.png"/>

这篇关于无法使用XAML中的相对路径引用图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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