Xaml - Bitmap UriSource - 绝对路径有效,相对路径无效,为什么? [英] Xaml - Bitmap UriSource - absolute path works, relative path does not, why?

查看:117
本文介绍了Xaml - Bitmap UriSource - 绝对路径有效,相对路径无效,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个 WPF &Xaml-noob,对于控制台应用程序,它对我有用,只需提供可执行文件的相对路径即可访问文件.对我来说,它似乎不适用于 xaml.(代码在底部)
绝对路径完美运行.

I'm, a WPF & Xaml-noob, for console applications it has worked for me to simply give the relative path to the executable to access a file. It doesn't seem to work in xaml for me. (code at the bottom)
Absolute path works perfectly.

是否可以在 WPF 应用程序中的 XAML 中通过简单地使用可执行文件目录的相对路径作为有效的 UriSource 来访问文件?如果是,如何,如果不是,为什么不?

我发现了以下问题,他们在那里讨论通过 Visual Studio 的添加现有项目"添加文件,所以这似乎是一个不同的问题.

I found the following question, where they were talking about adding the file via Visual Studio's "Add existing item", so it seems like a different issue.

我该怎么办将 WPF BitmapImage UriSource 属性设置为相对路径?

<Window.Icon>
  <!--absolute path works:-->
  <BitmapImage UriSource="C:\LongPath\SolutionFolder\ProjectFolder\bin\Debug\path4.ico" />

  <!--none of the following relative paths worked:-->
  <!--AppDomain.CurrentDomain.BaseDirectory returns the Debug-folder-->
  <!--<BitmapImage UriSource="path4.ico" />-->
  <!--<BitmapImage UriSource="../path4.ico" />-->
  <!--<BitmapImage UriSource="Debug/path4.ico" />-->
  <!--<BitmapImage UriSource="bin/Debug/path4.ico" />-->
  <!--<BitmapImage UriSource="../bin/Debug/path4.ico" />-->
  <!--<BitmapImage UriSource="../../bin/Debug/path4.ico" />-->
  <!--<BitmapImage UriSource="../Debug/path4.ico" />-->
</Window.Icon>

推荐答案

URI 可能会造成混淆,因为它们可以引用磁盘上的文件和应用程序中的资源.因此,当您打算将它放在磁盘上时,相对路径可以是应用程序中的资源.

URIs can be confusing, as they can refer to both files on disk and resources in the application. So a relative path could be to a resource in the app, when you intend it to be on disk.

您可以使用 siteoforigin 权限来强制使用相关文件 URI.本博客对此进行了更多解释,但这里有一个示例:

You can use the siteoforigin authority to force relative file URIs. This blog explains this more, but here an example:

pack://siteoforigin:,,,/path4.ico

这篇关于Xaml - Bitmap UriSource - 绝对路径有效,相对路径无效,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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