How to pass the UI Dispatcher to the ViewModel

前端 未结 16 657
独厮守ぢ
独厮守ぢ 2020-11-28 02:16

I\'m supposed to be able to access the Dispatcher that belongs to the View I need to pass it to the ViewModel. But the View should not know anything about the ViewModel, so

16条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 02:35

    I get the ViewModel to store the current dispatcher as a member.

    If the ViewModel is created by the view, you know that the current dispatcher at creation time will be the View's dispatcher.

    class MyViewModel
    {
        readonly Dispatcher _dispatcher;
        public MyViewModel()
        {
            _dispatcher = Dispatcher.CurrentDispatcher;
        }
    }
    

提交回复
热议问题