Update UI thread from portable class library

后端 未结 3 1626
庸人自扰
庸人自扰 2020-12-15 12:03

I have an MVVM Cross application running on Windows Phone 8 which I recently ported across to using Portable Class Libraries.

The view models are within the portable

3条回答
  •  一整个雨季
    2020-12-15 12:41

    Another option that could be easier is to store a reference to SynchronizationContext.Current in your class's constructor. Then, later on, you can use _context.Post(() => ...) to invoke on the context -- which is the UI thread in WPF/WinRT/SL.

    class MyViewModel
    {
       private readonly SynchronizationContext _context;
       public MyViewModel()
       {
          _context = SynchronizationContext.Current.
       }
    
       private void MyCallbackOnAnotherThread()
       {
          _context.Post(() => UpdateTheUi());
       }
    }
    

提交回复
热议问题