无法通过在Silverlight中提供相对路径找到图像

无法通过在Silverlight中提供相对路径找到图像,silverlight,xaml,imagebrush,Silverlight,Xaml,Imagebrush,我正在开发Silverlight应用程序,我很难在应用程序中设置图像的相对路径 这是我的项目目录的简要图片: 我的项目 L图像 L mountain1.jpg 精灵物体 L MountainFactory.cs GameScreen.xaml 最初,我使用GameScreen.xaml中的图像笔刷绘制了一个矩形 <Rectangle Name="rec_bg" HorizontalAlignment="Left" VerticalAlignment="Top" Width="800" He

我正在开发Silverlight应用程序,我很难在应用程序中设置图像的相对路径

这是我的项目目录的简要图片:

我的项目 L图像 L mountain1.jpg 精灵物体 L MountainFactory.cs GameScreen.xaml

最初,我使用GameScreen.xaml中的图像笔刷绘制了一个矩形

<Rectangle Name="rec_bg" HorizontalAlignment="Left" VerticalAlignment="Top" Width="800" Height="600">
    <Rectangle.Fill>
        <ImageBrush x:Name="ib_bg" ImageSource="./images/mountain1.jpg" ></ImageBrush>
    </Rectangle.Fill>
</Rectangle>
此方法将返回应用于rec_bg矩形对象的图像笔刷。 但是,此代码找不到指定的图像

有人知道如何解决这个问题吗?
谢谢。

这取决于图像的嵌入方式。我假设您将它设置为“内容”,因此它不嵌入DLL,而是嵌入XAP。在这种情况下,您只需相对于项目根访问它,因此您将使用:

“图像/山…”

(注意,您不使用前面的.,而是自动相对于应用程序的根)。如果将其设置为嵌入式资源,则需要使用流从DLL中提取它-我不建议这样做

短篇故事无聊:尝试删除前导/并确保图像的属性将其作为内容

ImageBrush brush = new ImageBrush();
BitmapImage image = new BitmapImage(new Uri("./images/mountain" + level + ".jpg", UriKind.Relative));
brush.ImageSource = image;