【问题标题】:Invoke a WPF PRISM dialog window of different module调用不同模块的 WPF PRISM 对话窗口
【发布时间】:2015-12-19 17:28:29
【问题描述】:

我有一个带有区域的 WPF PRISM 应用程序——菜单区域和工作区区域。单击菜单时,将在工作区区域内打开相应的视图/表单。我有不同的模块,如员工、组织、财务等......每个模块都有多个视图/表单。

当单击员工视图/表单(模块 - 员工)中的按钮时,我需要将财务模块中的视图/表单作为对话窗口打开。

有没有办法在 PRISM 中实现同样的效果?

[编辑 1]: 2 个模块是独立的模块。我不希望将财务模块的引用添加到 Employee 模块。再往下看,我可能需要从一个或多个财务视图中将 Employee 显示为对话窗口。

【问题讨论】:

    标签: wpf shell navigation prism


    【解决方案1】:

    当然 - 这里有两种方法供您考虑。

    选项 1

    Prism 的documentation 有一节使用他们的 CompositeCommand 类“使命令全局可用”。为此,请在两个程序集都可以引用的公共库中创建一个公共静态类:

    public static class GlobalCommands
    {
        public static CompositeCommand ShowFinanceFormCommand = new CompositeCommand();
    }
    

    在财务表单视图模型中,您将注册您的实际命令(具有显示表单的所有逻辑):

    public FinanceViewModel()
    {
        GlobalCommands.ShowFinanceFormCommand.RegisterCommand(this.ShowFinancePopupCommand); 
    }
    

    在员工视图模型中,将按钮的ICommand 绑定到ShowFinanceFormCommand 复合命令。

    public EmployeeViewModel()
    {
        this.EmployeeShowFinanceFormCommand = GlobalCommands.ShowFinanceFormCommand;
    }
    

    选项 2

    如果您需要以松散耦合的方式跨模块广播事件,请使用 Prism 的 EventAggregator 类。要实现这一点,请在两者都可以引用的公共程序集中创建一个ShowFinanceFormEvent。在员工视图模型中,按下按钮时发布一个事件。在财务视图模型中,订阅该事件并做出相应反应。

    // This sits in a separate module that both can reference
    public class ShowFinanceFormEvent : PubSubEvent<object>
    {
    }
    
    public class EmployeeViewModel
    {
        private readonly IEventAggregator eventAggregator;
    
        public EmployeeViewModel(IEventAggregator eventAggregator)
        {
            this.ShowFormCommand = new DelegateCommand(RaiseShowFormEvent);
            this.eventAggregator = eventAggregator;
        }
    
        public ICommand ShowFormCommand { get; }
    
        private void RaiseShowFormEvent()
        {
            // Notify any listeners that the button has been pressed
            this.eventAggregator.GetEvent<ShowFinanceFormEvent>().Publish(null);
        }
    }
    
    public class FinanceViewModel
    {
        public FinanceViewModel(IEventAggregator eventAggregator)
        {
            // subscribe to button click events
            eventAggregator.GetEvent<ShowFinanceFormEvent>().Subscribe(this.ShowForm);
    
            this.ShowFinanceFormRequest = new InteractionRequest<INotification>();
        }
    
        public InteractionRequest<INotification> ShowFinanceFormRequest { get; }
    
        private void ShowForm(object src)
        {
            // Logic goes here to show the form... e.g.
            this.ShowFinanceFormRequest.Raise(
                new Notification { Content = "Wow it worked", Title = "Finance form title" });
        }
    }
    

    【讨论】:

    • 感谢 Big AL 提供详细的答案和文档链接。目前我已经实施了选项 2(事件聚合),已阅读“解决方案命令”,并将为最终解决方案实施该策略(按照推荐并适合我的策略和需求)。
    【解决方案2】:

    选项 3,我认为更好的选择,就是使用 Prism 的导航框架。如果您需要显示对话框,则使用对话框服务并简单地将视图键传递给服务以确定在对话框中显示哪个视图。由于您使用的是唯一键而不是引用,因此您不必知道或关心视图的存在位置。

    我在这里的 PluralSight 课程中使用了类似的方法:

    https://app.pluralsight.com/library/courses/prism-showing-multiple-shells/table-of-contents

    查看对话服务演示。

    【讨论】:

    • 我订阅了 Pluralsight 的 msdn,但您的课程不包含在允许的课程中:'(
    猜你喜欢
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2020-01-27
    • 2021-06-05
    • 1970-01-01
    相关资源
    最近更新 更多