0

UI の画像プロパティにバインドされたビューモデルを使用していますが、ビューモデルには ImageSource プロパティが含まれています。次の関数を使用してそのプロパティを設定します

    private BitmapImage GetImageFromUri(Uri urisource)
    {
        if (urisource == null)
            return null;

        var image = new BitmapImage();
        image.BeginInit();
        image.UriSource = urisource;
        image.EndInit();
        image.Freeze(); //commenting this shows the image if the routine is called from the proper thread.

        return image;
   }

奇妙な理由で、次のコードでは、BitmapImage で Freeze を呼び出すと、メイン ウィンドウに表示されません。例外もクラッシュも発生しません。誰でもこれで私を助けることができますか? イメージ プロパティを非同期で設定しているため、UI スレッド以外のスレッドから GetImageFromUri 呼び出しが行われたと仮定して、作成したイメージを使用できるようにする必要があります。

4

3 に答える 3

3

フリーズする前に、BitmapImageのCacheOptionを設定してみてください。これが機能するかどうかを確認します-

var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = urisource;
image.EndInit();
image.Freeze();
于 2011-10-25T16:31:51.773 に答える