Prism

395 阅读1分钟

1、安装prism

nuget 安装Prism.DryIoc

2、修改App.xmal

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

xmlns:prism="prismlibrary.com/" 增加prism命名空间 根节点修改为prism:PrismApplication

3、修改App.xaml.cs

继承PrismApplication抽象类,实现CreateShell、RegisterTypes方法

   public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

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

        }
    }

4、目录约定

image.png 存在ViewModels、Views文件夹 Model文件已ViewModel结尾 添加命名空间,使用约定视图配置 image.png Model继承BindableBase即可 image.png

5、Module

新增类库Module,新增ModuleAProfile类,继承IModule

   public  class ModuleAProfile : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
           
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<UserControl1>();
        }
    }

在App.xmal.cs中CreateModuleCatalog指定Module目录

    protected override IModuleCatalog CreateModuleCatalog()
        {
            return new DirectoryModuleCatalog() { ModulePath= @".\Modules" };
        }

6、dialog

对话框的viewmodel需要继承IDialogAware,实现这个接口

public class UserLoginViewModel : BindableBase, IDialogAware
    {
        private string title { get; set; }

        public string Title { get { return title; } set { title = value; RaisePropertyChanged(); } }

        public event Action<IDialogResult> RequestClose;

        public DelegateCommand<string> SaveCommand { get; set; }
        public DelegateCommand<string> CancelCommand { get; set; }

        public UserLoginViewModel()
        {
            SaveCommand = new DelegateCommand<string>(Save);
            CancelCommand = new DelegateCommand<string>(Cancel);
        }

        private void Cancel(string obj)
        {
            DialogParameters param = new DialogParameters();
            param.Add("param", "我取消了");
            RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, param));
        }

        private void Save(string obj)
        {
            DialogParameters param = new DialogParameters();
            param.Add("param", "我确认了");
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK, param));
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            Title = parameters.GetValue<string>("Title");
        }
    }

注册dialog

containerRegistry.RegisterDialog<UserLogin>();

调用dialog

                DialogParameters param = new DialogParameters();
                param.Add("Title", "登录");
                dialogService.ShowDialog(obj, param, (call) =>
                  {
                      var param = call.Parameters.GetValue<string>("param");
                      MessageBox.Show(param);
                  });

7、发布订阅

继承PubSubEvent类

   public class NoticeEvent : PubSubEvent<string>
    {
        public NoticeEvent()
        {
        }
    }

发布 IEventAggregator 注入IEventAggregator实例,获取NoticeEvent事件示例 noticeEvent.Publish(obj);

 public class MainWindowViewModel : BindableBase
    {
        private IRegionManager regionManager;
        private IDialogService dialogService;
        private NoticeEvent noticeEvent;
        public MainWindowViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
        {
            ShowCommand = new DelegateCommand<string>(show);
            this.regionManager = regionManager;
            this.dialogService = dialogService;
            noticeEvent = eventAggregator.GetEvent<NoticeEvent>();
        }
        public DelegateCommand<string> ShowCommand { get; set; }

        private void show(string obj)
        {
            if (obj == "UserLogin")
            {
                DialogParameters param = new DialogParameters();
                param.Add("Title", "登录");
                dialogService.Show(obj, param, (call) =>
                  {
                      var param = call.Parameters.GetValue<string>("param");
                      MessageBox.Show(param);
                  });

            }
            else
            {
                NavigationParameters keys = new NavigationParameters();
                keys.Add("name", obj);
                regionManager.Regions["conentRegion"].RequestNavigate(obj, keys);
                noticeEvent.Publish(obj);
            }
        }
    }
}

调用Publish即可发布 订阅

  eventAggregator.GetEvent<NoticeEvent>().Subscribe(GetMsg);

GetMsg 即调用的函数