0

我有一个项目,我正在从 xml 文件加载相对图像 Uri。我正在加载这样的图像:

if (child.Name == "photo" &&
    child.Attributes["href"] != null &&
    File.Exists(child.Attributes["href"].Value))
{
    Image image = new Image();
    image.Source = new BitmapImage(new Uri(child.Attributes["href"].Value, UriKind.RelativeOrAbsolute));
    images.Add(image);
}

其中“子”对象是一个 XmlNode,看起来像这样

<photo name="info" href="Resources\Content\test.png"/>

在调试过程中,图像似乎充满了实际图像,但是当我想以任何方式查看它们时,它什么也没有显示。奇怪的是,当我将图像包含在我的项目中时它确实可以工作,但是我不想这样做,因为我使用 xml 文件的目的是因为你必须在更改后重新构建程序,所以它会丢失.

4

2 回答 2

1

不是完美的解决方案,但它仍然有效,我正在将相对 Uri 转换为像这样的绝对 Uri

if (child.Name == "photo" &&
    child.Attributes["href"] != null &&
    File.Exists(Environment.CurrentDirectory + child.Attributes["href"].Value))
{
    Image image = new Image();
    image.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + child.Attributes["href"].Value, UriKind.RelativeOrAbsolute));
    images.Add(image);
}

只需将 xml 中的所有 Uri 更改为具有前导斜杠即可。

于 2010-04-26T13:04:29.877 回答
0

嗯可以是这个简单的当前文件夹问题。VS检查project/Resources/Content/文件夹下的资源,program检查project/bin/Debug/Resources/Content/文件夹下的资源。

于 2010-04-26T11:39:01.667 回答