c# - 无法使用 Prism 从回调方法内部导航

标签 c# wpf module navigation prism

我有一个使用 WPF 和 Prism 的小型应用程序。我有我的外壳和两个模块。我可以以“正常方式”(例如通过单击按钮)在它们之间成功导航,因此我知道它们已正确连接以进行导航。但是,如果我执行一些在完成时触发事件的异步操作,我就无法从该事件处理程序内部进行导航。我尝试的最后一件事是使用事件聚合将事件发布回 UI 线程,但它仍然没有导航。事件订阅者成功获取事件并触发 RequestNavigate(...) 但 UI 没有更新。

现在,一些代码: 我的第一个模块 LoginModule 的 View 模型:

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;
    }
    ...
}

我的第二个模块 RosterModule 的 ViewModel:

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");
    }
}

有什么关于我可能做错的提示吗?

最佳答案

来自 OP,

好的,所以在发布几分钟后,我重读了昨天遇到的一篇文章,发现了一些我错过的东西......

http://neverindoubtnet.blogspot.com/2009/05/event-aggregator-in-prism-explorer.html

他们解释说 Subscribe 方法的重载之一包括 ThreadOption。

所以:

_eventAggregator.GetEvent<UserAuthenticatedEvent>()
    .Subscribe(
    o =>
    {
        Console.WriteLine("Requesting navigation...");
        _regionManager.RequestNavigate(
            RegionNames.ContentRegion,
            new Uri(WellKnownViewNames.RosterView, UriKind.Relative));
    });

成为:

_eventAggregator.GetEvent<UserAuthenticatedEvent>()
    .Subscribe(
    o =>
    {
        Console.WriteLine("Requesting navigation...");
        _regionManager.RequestNavigate(
            RegionNames.ContentRegion,
            new Uri(WellKnownViewNames.RosterView, UriKind.Relative));
    }, 
    ThreadOption.UIThread);

现在可以了!

希望这对以后的其他人有所帮助。

尽情享受吧!

关于c# - 无法使用 Prism 从回调方法内部导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19407336/

相关文章:

module - 模块中的意外赋值语句

java - 如何使库在 java 中可替换?

c# - LINQ to Entity Any() 及相关对象集合

c# - 获取常规 ConfigurationErrorsException

c# - 在不更改目标类型的情况下以最高精度序列化 float

wpf - 如何为 WPF 创建矢量图像

php - Apache 不执行 php 文件,但启用了 mod_php5

c# - ServicePointManager.ServerCertificateValidationCallback 没有被击中

c# - 如何从 WPF KeyDown 事件中获取普通字符?

c# - 执行 RenderTransform(缩放和平移)后如何获得 UIElement 的新位置和大小?