我有一个使用 WPF 和 Prism 的小型应用程序。我有我的外壳和两个模块。我可以以“正常方式”(例如通过单击按钮)在它们之间成功导航,因此我知道它们已正确连接以进行导航。但是,如果我执行一些在完成时触发事件的异步操作,我将无法从该事件处理程序内部导航。我尝试的最后一件事是使用事件聚合将事件发布回 UI 线程,但它仍然无法导航。事件的订阅者成功获取事件并触发 RequestNavigate(...) 但 UI 没有更新。
现在,一些代码:我的第一个模块的视图模型LoginModule
:
public class LoginViewModel : ViewModelBase, ILoginViewModel, INavigationAware
{
...
[ImportingConstructor]
public LoginViewModel(IRegionManager regionManager, IUnityContainer container, IEventAggregator eventAggregator)
{
_regionManager = regionManager;
_container = container;
_eventAggregator = eventAggregator;
}
private DelegateCommand _Login;
public DelegateCommand Login
{
get
{
if (_Login == null)
_Login = new DelegateCommand(() => LoginHandler());
return _Login;
}
}
private void LoginHandler()
{
_client = new JabberClient();
_client.Server = "gmail.com";
_client.User = Username;
_client.Password = Password;
...
_client.OnAuthenticate += client_OnAuthenticate;
_client.Connect();
}
private void client_OnAuthenticate(object sender)
{
Console.WriteLine("Authenticated!");
_eventAggregator.GetEvent<UserAuthenticatedEvent>().Publish("");
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
...
}
我的第二个模块的 ViewModel RosterModule
:
public class RosterViewModel : IRosterViewModel, INavigationAware
{
private readonly IEventAggregator _eventAggregator;
private readonly IRegionManager _regionManager;
[ImportingConstructor]
public RosterViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
{
_regionManager = regionManager;
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<UserAuthenticatedEvent>().Subscribe(o =>
{
Console.WriteLine("Requesting navigation...");
_regionManager.RequestNavigate(RegionNames.ContentRegion, new Uri(WellKnownViewNames.RosterView, UriKind.Relative));
});
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
Console.WriteLine("I'm here at the RosterViewModel");
}
}
关于我可能做错了什么的任何提示?