2016-11-03 5 views
0

バインディングのBitmapImageデータは、私はシングルトンのBitmapImageを持っていると私は私のXAMLビューにバインドしようとしていると私が得た:WPF - シングルトン

The calling thread cannot access this object because it is owned by another thread 

ここに私のコード:

BitmapImage bitmap = new BitmapImage(); 
bitmap.BeginInit(); 
bitmap.UriSource = new Uri(fullFilePath, UriKind.Absolute); 
bitmap.EndInit(); 
MySingleton.Instance.image = bitmap; 

マイシングルトンを:

private BitmapImage _image; 
    public BitmapImage image 
    { 
    get { return _image; } 
    set 
    { 
     _image = value; 
     PropertyChanged(this, new PropertyChangedEventArgs(nameof(image))); 
    } 
    } 

そして、私のXAML:

<Image Source="{Binding image, Source={x:Static module:MySingleton.Instance}}" Name="TestImage" HorizontalAlignment="Center" Height="Auto" VerticalAlignment="Center" Width="Auto"></Image> 

は私がbitmap.Freeze();を試みたが、エラーが発生しました:

Freezable cannot be frozen

それはmeanlessだかない場合、私は知らないが、私はのWebSocketのonMessageイベントでビットマップをインスタンス化します。

+0

'ローカルファイルパスをfullFilePath'ていますか? – Clemens

+0

@Clemensいいえ、私はバインドせずにコードをテストし、 "Application.Current.Dispatcher.Invoke(()"とLupu Silviuによって与えられていますが、動作していますが、なぜバインディングが機能していないのかまだわかりませんでした。 – Safe

+0

ビットマップの 'CacheOption'を' EndInit() 'の前に' BitmapCacheOption.OnLoad'に設定しようとしてください。 – dymanoid

答えて

1

あなたのコードはDispatcher内に配置する必要があります。

Application.Current.Dispatcher.Invoke(() => 
{ 
    //(your code here) 
}); 

または

Application.Current.Dispatcher.BeginInvoke((Action)(() => 
{ 
    //(your code here) 
})); 
+0

ありがとうございました!私はすでにこれを見ましたが、間違ったインポートを選択しました:S コードは次のとおりです。今は動作していますが、画像はまだ表示されません。 画像はデータなしの生コードで表示されます。 – Safe

+0

私の悪い結束が働いていた、私はちょうど私のテストxDに失敗しました。 – Safe

関連する問題