Reputation: 373
I have a singleton BitmapImage and I'm trying to bind it to my xaml view and i got :
The calling thread cannot access this object because it is owned by another thread
Here my code :
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(fullFilePath, UriKind.Absolute);
bitmap.EndInit();
MySingleton.Instance.image = bitmap;
My singleton :
private BitmapImage _image;
public BitmapImage image
{
get { return _image; }
set
{
_image = value;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(image)));
}
}
And my xaml :
<Image Source="{Binding image, Source={x:Static module:MySingleton.Instance}}" Name="TestImage" HorizontalAlignment="Center" Height="Auto" VerticalAlignment="Center" Width="Auto"></Image>
I tried bitmap.Freeze();
but got the error:
Freezable cannot be frozen
I don't know if it's meanless or not but I instanciate the bitmap in a websocket onmessage event.
Upvotes: 0
Views: 158
Reputation: 1165
Your code has to be placed inside a Dispatcher.
Application.Current.Dispatcher.Invoke(() =>
{
//(your code here)
});
or
Application.Current.Dispatcher.BeginInvoke((Action)(() =>
{
//(your code here)
}));
Upvotes: 1