1

Windows Metroアプリケーション(リリースプレビュー)では、データポイントから動的に画像を作成し、データバインディングを使用して画像を表示しています。

画像の作成はかなり重い処理なので、タスクで行ってほしいです。したがって、私のプロパティは現在次のようにコーディングされています。

public ImageSource Diagram
{
    get
    {
        if (this.diagram == null)
        {
             DiagramGenerator.GetDiagram(this.DataPoints, this.Width, this.Height).ContinueWith((t) =>
             {
                 this.Diagram = t.Result;
             }
        }
        return this.diagram;
    }
    set
    {
        this.SetProperty(ref this.diagram, value);
    }
}

そして、DiagramGeneratorは次のようになります。

public static async Task<ImageSource> DiagramGenerator.GetDiagram(List<DataPoint> dataPoints, int width, int height)
{
    WriteableBitmap bmp = BitmapFactory.New(width, height);

    // Build the image...

    return bmp;
}

Xamlでは、バインディングは非常に単純です

<Image Source="{Binding Diagram}" Stretch="UniformToFill"/>

残念ながら、上記のコードを使用しても機能しません。

OnPropertyChangedメソッドで発生した例外が発生します(たとえば、上記のthis.Diagram = t.Resultの後):「アプリケーションは、別のスレッド用にマーシャリングされたインターフェイスを呼び出しました。」

次に、以下を使用してUIスレッドにマーシャルしようとしました。

Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Norma、()=> {this.Diagram = t.Result;});

しかし、Window.Currentがnullであるため、これも機能しません。

どうすればこれを修正できますか?

アドバイスありがとうございます。

4

1 に答える 1

0

UI スレッドを使用しているときに Window.Current.Dispatcher を取得し、後で使用するために保存することができます (たとえば、Diagram オブジェクトを作成するときなど)。または、Dispatcher は DependencyObject に格納されているため、そこから取得できます (つまり、任意の UIElement オブジェクトにあります)。

于 2012-07-17T01:37:16.523 に答える