ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#-Wpf-Prism.DryIoc-【5】事件聚合器

2022-04-20 17:00:53  阅读:204  来源: 互联网

标签:eventAggregator C# DryIoc Prism using Wpf public


【1】先完成基本的环境搭建,可参考C#-WPF-Prism.DryIoc-【1】环境搭建 - 轻吟浅唱,蓦然花开 - 博客园 (cnblogs.com)

【2】事件聚合器可用于跨ViewModel的消息传递,所以我们需要先定义一个消息实体Message.cs(根据自己的需要写)放到项目的Events文件夹下,并创建一个MessageEvent继承于泛型类PubSubEvent。代码如下

namespace Wpf_Prism.DryIoc_EventAggregator.Events
{
    public class Message
    {
        public long Id { get; set; }
        public string Content { get; set; }
    }
}
using Prism.Events;
namespace Wpf_Prism.DryIoc_EventAggregator.Events
{
    public class MessageEvent:PubSubEvent<Message>
    {
    }
}

【3】本项目结构如下图,再Views文件夹中建立了View1和View2两个用户控件和一个窗体MainView,而ViewModels下也分别建立了对应的ViewModel

 

 【4】编辑MainView.xaml 在上面放了三个按钮并绑定了命令和参数,用来切换页面和推送消息

<Window x:Class="Wpf_Prism.DryIoc_EventAggregator.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:local="clr-namespace:Wpf_Prism.DryIoc_EventAggregator.Views"
        prism:ViewModelLocator.AutoWireViewModel="True"
        mc:Ignorable="d"
        Title="MainView" Height="450" Width="800">
    <DockPanel>
        <UniformGrid Columns="3" DockPanel.Dock="Top">
            <Button Content="打开View1" Command="{Binding OpenViewCommand}" CommandParameter="View1"/>
            <Button Content="打开View2" Command="{Binding OpenViewCommand}" CommandParameter="View2"/>
            <Button Content="发送消息" Command="{Binding SendMessageCommand}"/>
        </UniformGrid>
        <ContentControl prism:RegionManager.RegionName="MainContentRegion"/>
    </DockPanel>
</Window>

【5】编辑MainViewModel.cs 添加对应的命令,以及注入需要使用的IRegionManager 和 IEventAggregator接口

using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Wpf_Prism.DryIoc_EventAggregator.Events;

namespace Wpf_Prism.DryIoc_EventAggregator.ViewModels
{
    public class MainViewModel
    {
        private readonly IRegionManager regionManager;
        private readonly IEventAggregator eventAggregator;

        public DelegateCommand<string> OpenViewCommand { get;private set; }
        public DelegateCommand SendMessageCommand { get;private set; }
        public MainViewModel(IRegionManager regionManager,IEventAggregator eventAggregator)
        {
            this.regionManager = regionManager;
            this.eventAggregator = eventAggregator;
            OpenViewCommand = new DelegateCommand<string>(OpenView);
            SendMessageCommand = new DelegateCommand(SendMessage);
        }

        /// <summary>
        /// 发送消息 Hello
        /// </summary>
        private void SendMessage()=> eventAggregator.GetEvent<MessageEvent>().Publish(new Message { Content = "Hello" });

        /// <summary>
        /// 导航
        /// </summary>
        /// <param name="obj"></param>
        private void OpenView(string obj) => regionManager.Regions["MainContentRegion"].RequestNavigate(obj);
    }
}

【6】编辑View1ViewModel 和View2ViewModel代码如下

using Prism.Events;
using System.Windows;
using Wpf_Prism.DryIoc_EventAggregator.Events;

namespace Wpf_Prism.DryIoc_EventAggregator.ViewModels
{
    public class View1ViewModel
    {
        private readonly IEventAggregator eventAggregator;

        public View1ViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            //订阅
            eventAggregator.GetEvent<MessageEvent>().Subscribe(ReceivedMessage);
        }

        /// <summary>
        /// 接收到消息
        /// </summary>
        /// <param name="obj"></param>
        private void ReceivedMessage(Message obj) => MessageBox.Show("View1收到消息:" + obj.Content);
    }
}
using Prism.Events;
using System.Windows;
using Wpf_Prism.DryIoc_EventAggregator.Events;

namespace Wpf_Prism.DryIoc_EventAggregator.ViewModels
{
    public class View2ViewModel
    {
        private readonly IEventAggregator eventAggregator;

        public View2ViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            eventAggregator.GetEvent<MessageEvent>().Subscribe(ReceivedMessage);
        }

        private void ReceivedMessage(Message obj)
        {
            MessageBox.Show("View2收到消息:" + obj.Content+"\r\n后续不会收到消息");
            //取消订阅
            eventAggregator.GetEvent<MessageEvent>().Unsubscribe(ReceivedMessage);
        }
    }
}

【7】在app.xaml.cs中将View1,View1ViewModel 和View2 ,View2ViewModel注册到容器

using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Wpf_Prism.DryIoc_EventAggregator.ViewModels;
using Wpf_Prism.DryIoc_EventAggregator.Views;

namespace Wpf_Prism.DryIoc_EventAggregator
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainView>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<View1, View1ViewModel>();
            containerRegistry.RegisterForNavigation<View2, View2ViewModel>();
        }
    }
}

【8】运行项目,效果如下:

完成,谢谢!

标签:eventAggregator,C#,DryIoc,Prism,using,Wpf,public
来源: https://www.cnblogs.com/nickyangmiracle/p/16170080.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有