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/ フォルダーの下のリソースをチェックし、プログラムは project/bin/Debug/Resources/Content/ フォルダーの下のリソースをチェックします。

于 2010-04-26T11:39:01.667 に答える