gpt4 book ai didi

wpf - 使用 IEventAggregator,是否可以先发布事件然后订阅它?

转载 作者:行者123 更新时间:2023-12-03 11:03:06 25 4
gpt4 key购买 nike

也许我的实现方法是错误的,但我在这里:

我目前正在使用 MVVM 和 Prism 开发一个应用程序,带有登录对话框和多个模块:

  • MainWindowViewModel订阅事件:_ea.GetEvent<RegisterMenuItemEvent>().Subscribe(AddMenuItem, true);检查是否必须将模块名称添加到 ListView ;
  • 在启动时,它会从一个文件夹中动态加载所有模块:
  • 每个 IModule 类监听一个 LoginEvent:_ea.GetEvent<LoginEvent>().Subscribe(CheckRoles, true);检查用户是否有权导航到模块的 View ;
  • 如果它符合,则使用 CheckRoles()方法它发布一个将模块注册为 ListView 项的事件:_ea.GetEvent<RegisterMenuItemEvent>().Publish(new MenuItem(null, View, ModuleName, GroupName));
  • 登录后
  • 它根据每个模块的给定角色将模块注册为 ListView 菜单项:_ea.GetEvent<LoginEvent>().Publish(new LoginInfo(ctxUser.Username, ctx.UserRole.Where(r => r.UserId == ctxUser.Id).Select(r => r.Role.Name).ToList()));
  • 将经过身份验证的用户名设置为 MainWindowView

  • 上面的所有内容都按照确切的顺序执行。

    我的问题是,如何订阅已经发布的事件?:
  • 这已在 MainWindowViewModel _ea.GetEvent<MessageEvent>().Publish("Username"); 中执行
  • 这是在模块的 ViewModel 中执行的 _ea.GetEvent<MessageEvent>().Subscribe(GetUsername); ,但它不起作用。
  • 最佳答案

    My question is, how can I subscribe to an event that has been already published?



    你不能。好吧,您确实可以订阅该事件,但如果您明白我的意思,您将不会收到任何已发布的事件。

    因此,如果您想确保不会错过任何事件,则需要确保在任何其他组件发布事件之前订阅该事件。

    关于wpf - 使用 IEventAggregator,是否可以先发布事件然后订阅它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46037109/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com