我有一个单独的BitmapImage,我正试图将它绑定到我的xaml视图,我得到了:
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无法冻结
我不知道它是否无意义但是我在websocket onmessage事件中实现了位图。
答案 0 :(得分:1)
您的代码必须放在Dispatcher中。
xs:string
或
Application.Current.Dispatcher.Invoke(() =>
{
//(your code here)
});