C# 相对URI在BitmapImage中不起作用
我有一个通过绑定加载图像以供以后使用的方法 当我按如下方式设置它时,它会工作:C# 相对URI在BitmapImage中不起作用,c#,xaml,C#,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)); } 但是,当我换成简单地
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));
}
但是,当我换成简单地使用:
public static ImageSource LoadImage()
{
return new BitmapImage(
new Uri("/subfolder/sprite.png", UriKind.Relative));
}
XAML不显示图像。我查看了Environment.CurrentDirectory
,它返回“C:/User/Name/Documents/visualstudio 2015/Projects/Project/bin/debug/”
我在谷歌上搜索了一个解决方案,但似乎什么都看不到。如果您希望将图像放在输出文件夹中。将图像添加到项目中,在构建操作中选择“内容”,并在复制到输出目录选项中选择“始终复制”
您将在输出中获得图像,并且可以使用相对URI命令加载图像。您应该从程序集资源文件加载图像 确保图像文件位于名为
子文件夹的Visual Studio项目文件夹中
,并将其生成操作设置为资源
。然后通过以下方式加载:
如果您在项目中的任何地方都没有使用图像,并且图像被标记为“资源”,那么它将不会在最终构建中包含您的图像。单击您的图像,在“构建操作”字段中选择“内容”。您确定图像在那里吗?路径是否正确(您是否尝试过
/sub/sprite
或sub/sprite
)?您是否尝试将精灵放入项目中,并确保将其设置为“始终复制”或“更新时复制”?是的,我确信它存在,因为它在我使用绝对路径时有效,而在尝试相对路径时无效。我也尝试过前导句点,去掉前导斜杠,运气不好。我似乎无法让相对Uri
对象正常工作。它只在我使用绝对路径时有效。
public static ImageSource LoadImage()
{
return new BitmapImage(new Uri("pack://application:,,,/subfolder/sprite.png"));
}