【发布时间】:2016-11-28 07:57:13
【问题描述】:
我有一种通过绑定加载图像供以后使用的方法。
当我按如下方式设置它时,它可以工作:
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/Visual Studio 2015/Projects/Project/bin/debug/"
我用谷歌搜索了一个解决方案,但似乎什么也没看到。
【问题讨论】:
-
你确定图片在那里?路径是否正确(您是否尝试过
./sub/sprite或sub/sprite)?您是否尝试过将精灵放入项目中,并确保将其设置为“始终复制”或“更新时复制”? -
是的,我相信它就在那里,因为它在我使用绝对路径时有效,而不是在尝试相对路径时。我也尝试过前导句号,并删除前导斜杠,没有运气。
-
我似乎无法让相对的
Uri对象正常工作。它仅在我使用绝对路径时才有效。 -
解决方法:
return new BitmapImage(new Uri(System.IO.Path.GetFullPath(@"/subfolder/sprite.png")));