我有一个加载图像的方法,以便以后通过绑定使用。相对URI不能在BitmapImage中工作
当我将它设置如下原理:
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')?你有没有尝试将精灵放入项目中,并确保将其设置为“始终复制”或“更新时复制”? – devRicher
是的,我相信它在那里,因为它在我使用绝对路径时起作用,而不是在尝试相对路径时。我也尝试了领先的时期,并删除了主要的斜线,没有运气。 – Classtronaut
我似乎无法得到相对的'Uri'对象来工作。它只适用于我使用绝对路径。 –