gpt4 book ai didi

wpf - Xaml - Bitmap UriSource - 绝对路径有效,相对路径无效,为什么?

转载 作者:行者123 更新时间:2023-12-04 10:11:37 27 4
gpt4 key购买 nike

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

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

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

How can I set the WPF BitmapImage UriSource property to a relative path?

<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 可能会令人困惑,因为它们可以引用磁盘上的文件和应用程序中的资源。因此,当您打算将它放在磁盘上时,相对路径可能是应用程序中的资源。

您可以使用 siteoforigin强制相对文件 URI 的权限。 This blog更多地解释了这一点,但这里有一个例子:

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

关于wpf - Xaml - Bitmap UriSource - 绝对路径有效,相对路径无效,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6111470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com