EventAggregator.PublishOnUIThread 来自 Caliburn.Micro 抛出:System.InvalidOperationException:'Not initialized with dispatcher.'
EventAggregator.PublishOnUIThread from Caliburn.Micro throws : System.InvalidOperationException: 'Not initialized with dispatcher.'
我正在开发 UWP 应用程序。我正在为 MVVM 使用 caliburn.micro 框架。
我已经覆盖了 ShellViewModel 的 OnViewLoaded(object view)
方法来对异步登录方法进行等待调用,如下所示:
protected async override void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
if (GetView() is UIElement page)
{
page.KeyboardAccelerators.Add(_altLeftKeyboardAccelerator);
page.KeyboardAccelerators.Add(_backKeyboardAccelerator);
}
await StartLoginProcess();
}
private async Task StartLoginProcess()
{
/// validations go here
await _eventAggregator.PublishOnUIThreadAsync(new AuthenticationFailedMessage()).ConfigureAwait(false);
}
当我尝试 运行 应用程序时,它抛出以下异常:
exception at login
起初我认为ConfigureAwait(false)
在StartLoginProcess()
中的用法是错误的,但删除它没有任何区别。
有人遇到过这种问题吗?
任何帮助将不胜感激。
_eventAggregator.PublishOnCurrentThread(message);
解决了问题。我想我是从错误的上下文中调用了 _eventAggregator.PublishOnUIThread(message)
。它基本上需要从 UI 线程本身内部调用,而在我的情况下并非如此。
我正在开发 UWP 应用程序。我正在为 MVVM 使用 caliburn.micro 框架。
我已经覆盖了 ShellViewModel 的 OnViewLoaded(object view)
方法来对异步登录方法进行等待调用,如下所示:
protected async override void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
if (GetView() is UIElement page)
{
page.KeyboardAccelerators.Add(_altLeftKeyboardAccelerator);
page.KeyboardAccelerators.Add(_backKeyboardAccelerator);
}
await StartLoginProcess();
}
private async Task StartLoginProcess()
{
/// validations go here
await _eventAggregator.PublishOnUIThreadAsync(new AuthenticationFailedMessage()).ConfigureAwait(false);
}
当我尝试 运行 应用程序时,它抛出以下异常: exception at login
起初我认为ConfigureAwait(false)
在StartLoginProcess()
中的用法是错误的,但删除它没有任何区别。
有人遇到过这种问题吗? 任何帮助将不胜感激。
_eventAggregator.PublishOnCurrentThread(message);
解决了问题。我想我是从错误的上下文中调用了 _eventAggregator.PublishOnUIThread(message)
。它基本上需要从 UI 线程本身内部调用,而在我的情况下并非如此。