I am working on a WinRT / Windows Store application written in C # that uses MVVM Light. I am trying to change the MVVM Light OnPropertyChanged () method so that event notifications always appear in the user interface thread, otherwise the interface elements bound to my view model properties will skip event notifications because they should occur in the user interface stream. I am currently trying to use this code:
protected void OnPropertyChanged(string propertyName = null)
{
var eventHandler = PropertyChanged;
if (eventHandler != null)
{
var dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;
dispatcher.RunAsync(dispatcher.CurrentPriority,
() =>
{
eventHandler(this, new PropertyChangedEventArgs(propertyName));
});
}
}
I used this code before embedding the code in the user interface thread. However, in this case, I get the following exception:
"System.Exception" Common_WinStore.DLL
WinRT: , , marshalled .
, CoreWindow.Dispatcher , , UI, . marshaling SO, RunAsync() . , , .
Dispatcher.RunAsync() ?
. - , , WriteableBitmap . , , , . , ConfigureAwait (false) , . COM Exception, MVVM Light OnNotifyPropertyChanged() . . , , , , , .
UPDATE: AsyncEx, . . , . , , JPEG, SQLite-. , async JPEG WriteableBitmap. - , , , , .
AsyncManualResetEvent, . set JPEG bytes . async- , . , , - :
private AsyncManualResetEvent _asyncManResetEvent = new AsyncManualResetEvent(false);
private byte[] _thumbnailJpegBytes;
public byte[] ThumbnailJpegBytes
{
get
{
return this._thumbnailJpegBytes;
}
set
{
SetProperty(ref this._thumbnailJpegBytes, value, "ThumbnailJpegBytes");
_asyncManResetEvent.Set();
}
}
[Ignore]
public INotifyTaskCompletion<WriteableBitmap> ThumbnailAsync
{
get;
private set;
}
async private Task<WriteableBitmap> ConvertJpegBytesToThumbnail()
{
await this._asyncManResetEvent.WaitAsync();
return await Misc.JpegBytesToBitmap(ThumbnailJpegBytes);
}