相对 URI 在 BitmapImage 中不起作用

Cla*_*aut 5 c# wpf xaml

我有一种通过绑定加载图像供以后使用的方法。

当我按如下方式设置它时,它可以工作:

public static ImageSource LoadImage()
{
    return new BitmapImage(
        new Uri("C:/User/Name/Documents/Visual Studio 2015/Projects/Project/bin/debug/subfolder/sprite.png", UriKind.Absolute));
}
Run Code Online (Sandbox Code Playgroud)

但是,当我切换到简单地使用时:

public static ImageSource LoadImage()
{
    return new BitmapImage(
        new Uri("/subfolder/sprite.png", UriKind.Relative));
}
Run Code Online (Sandbox Code Playgroud)

XAML 不显示图像。我看了看Environment.CurrentDirectory它又回来了"C:/User/Name/Documents/Visual Studio 2015/Projects/Project/bin/debug/"

我已经用谷歌搜索了一个解决方案,但似乎什么也看不到。

Jos*_* B. 0

如果您希望图像位于输出文件夹中。将图像添加到项目中,在“构建操作”中选择“内容”,然后在“复制到输出目录”选项中选择“始终复制”。

您将在输出中获得图像,并且可以使用相对 URI 命令加载它。