Jen*_*ens 7 wpf xaml relative-path
我是一个WPF和Xaml-noob,对于控制台应用程序,它对我来说只是给出了可执行文件的相对路径来访问文件.它对我来说似乎不适用于xaml.(底部代码)
绝对路径完美无缺.
是否可以在WPF应用程序的XAML中通过简单地使用可执行文件目录的相对路径作为有效的UriSource来访问文件?如果是的话,如果不是,为什么不呢?
我发现了以下问题,他们在谈论通过Visual Studio的"添加现有项目"添加文件,所以这似乎是一个不同的问题.
如何将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>
Run Code Online (Sandbox Code Playgroud)
Cod*_*ked 11
URI可能会令人困惑,因为它们可以引用磁盘上的文件和应用程序中的资源.因此,当您打算将它放在磁盘上时,相对路径可能是应用程序中的资源.
您可以使用siteoforigin权限强制相关文件URI.这篇博客更多地解释了这个,但这里有一个例子:
pack://siteoforigin:,,,/path4.ico
Run Code Online (Sandbox Code Playgroud)