wpf prism框架(7.2) Module之间的通信

做一个ModuleA与ModuleB之间的通信

1.新建一个ModuleA:

wpf prism框架(7.2) Module之间的通信

ViewAViewModel

 public class ViewAViewModel:BindableBase
    {
        IEventAggregator _ea;

        private string _message="default" ;
        public string Message
        {
            get { return _message; }
            set { SetProperty(ref _message, value); }
        }

        public DelegateCommand SendMessageCommand { get; private set; }

        public ViewAViewModel(IEventAggregator ea)
        {
            _ea = ea;
            SendMessageCommand = new DelegateCommand(()=> { _ea.GetEvent<MessageSendEvent>().Publish(Message); });
        }
    }

View.xaml

  <StackPanel>
        <Button Content="btn1" Margin="20" Command="{Binding SendMessageCommand}"/>
        <TextBox Margin="20" Text="{Binding Message}"/>
    </StackPanel>

ModuleAClass

public class ModuleAClass: IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            var regionManager = containerProvider.Resolve<IRegionManager>();
            regionManager.RegisterViewWithRegion("RegionModuleA", typeof(ViewA));
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }
    }

 2.新建一个ModuleB:

wpf prism框架(7.2) Module之间的通信

ViewBViewModel:

public class ViewBViewModel : BindableBase
    {
        IEventAggregator _ea;
        private string _getValue;
        public string GetValue
        {
            get { return _getValue; }
            set { SetProperty(ref _getValue, value); }
        }

        public ViewBViewModel(IEventAggregator ea)
        {
            _ea = ea;       
            _ea.GetEvent<MessageSendEvent>().Subscribe((x) => { GetValue = x; });
        }
    }

ViewB

<StackPanel>
        <TextBlock Margin="20" Text="{Binding GetValue}" />
    </StackPanel>

ModuleBClass

public  class ModuleBClass:IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            var regionManager = containerProvider.Resolve<IRegionManager>();
            regionManager.RegisterViewWithRegion("RegionModuleB", typeof(ViewB));
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }
    }

 

3.新建主窗体:

wpf prism框架(7.2) Module之间的通信

 

shell.xaml:

 <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ContentControl prism:RegionManager.RegionName="RegionModuleA"/>
        <ContentControl prism:RegionManager.RegionName="RegionModuleB" Grid.Column="1"/>
    </Grid>

App .xaml:

<prism:PrismApplication x:Class="PrismDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:PrismDemo"
             xmlns:prism="http://prismlibrary.com/"
             >
    <Application.Resources>
         
    </Application.Resources>
</prism:PrismApplication>

App.xaml.cs

 public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        { 
        return Container.Resolve<Shell>();
        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }
        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            moduleCatalog.AddModule<ModuleA.ModuleAClass>();
            moduleCatalog.AddModule<ModuleB.ModuleBClass>();

        }
    }

4.新建一个跨试图通信工具

wpf prism框架(7.2) Module之间的通信

 public class MessageSendEvent : PubSubEvent<string>
    {
    }

完成!!

效果:

wpf prism框架(7.2) Module之间的通信

wpf prism框架(7.2) Module之间的通信

上一篇:window10安装dubbo-admin


下一篇:Nuget:aliyun-openapi-sdk