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

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

做一个ModuleA与ModuleB之间的通信

1.新建一个ModuleA:

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

ViewAViewModel

wpf prism框架(7.2) Module之间的通信
 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); });
        }
    }
wpf prism框架(7.2) Module之间的通信

View.xaml

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

ModuleAClass

wpf prism框架(7.2) Module之间的通信
public class ModuleAClass: IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            var regionManager = containerProvider.Resolve<IRegionManager>();
            regionManager.RegisterViewWithRegion("RegionModuleA", typeof(ViewA));
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {

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

 2.新建一个ModuleB:

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

ViewBViewModel:

wpf prism框架(7.2) Module之间的通信
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; });
        }
    }
wpf prism框架(7.2) Module之间的通信

ViewB

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

ModuleBClass

wpf prism框架(7.2) Module之间的通信
public  class ModuleBClass:IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            var regionManager = containerProvider.Resolve<IRegionManager>();
            regionManager.RegisterViewWithRegion("RegionModuleB", typeof(ViewB));
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {

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

 

3.新建主窗体:

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

 

shell.xaml:

wpf prism框架(7.2) Module之间的通信
 <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ContentControl prism:RegionManager.RegionName="RegionModuleA"/>
        <ContentControl prism:RegionManager.RegionName="RegionModuleB" Grid.Column="1"/>
    </Grid>
wpf prism框架(7.2) Module之间的通信

App .xaml:

wpf prism框架(7.2) Module之间的通信
<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>
wpf prism框架(7.2) Module之间的通信

App.xaml.cs

wpf prism框架(7.2) Module之间的通信
 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>();

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

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

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

 public class MessageSendEvent : PubSubEvent<string>
    {
    }

完成!!

效果:

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

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

上一篇:Matlab中数组元素引用——三种方法


下一篇:MongoDB:更改数据库位置(Windows)