WPF Prism构建模块式开发框架

WPF Prism

  • Prism框架扩展安装
    • 资源项目
    • Prism主项目
    • 模块项目

Prism框架扩展安装

Prism已经更新到7.x。打开VS2017,在扩展和更新里面安装Prism模板。
在这里插入图片描述
新建3个项目,1个主项目,1个模块项目,最后1个资源项目(提供界面样式)。项目之间通过prism自带的ioc注入,达到解耦,项目之间不需要互相引用。
WPF Prism构建模块式开发框架_第1张图片

资源项目

添加“资源字典" DefaultStyle.xaml文件,代码如下:


    

Prism主项目

打开App.xaml.cs文件修改如下:

using DevExpress.Xpf.Core;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Unity;
using System.Windows;
using TEST500ServiceMonitor.Views;

namespace TEST500ServiceMonitor
{
    /// 
    /// Interaction logic for App.xaml
    /// 
    public partial class App : PrismApplication
    {
        public App()
        {
        //使用主题(devexpress),
            ApplicationThemeHelper.ApplicationThemeName = Theme.Office2016ColorfulName;
            ApplicationThemeHelper.UseLegacyDefaultTheme = false;
            //启动splash
            DXSplashScreen.Show();
        }

        protected override Window CreateShell()
        {
            return Container.Resolve();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
        //定义模块加载位置,相对路径(此类方式加载速度稍慢但方便,也可使用指定方式加载,此处不做详细解释,可查看官方文档或样例)
            return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
        }
    }
}

App.xaml文件中使用资源字典项目(需要先编译下,再引用,否则找不到路径),代码如下:


    
        
            
                
            
        
    


MainWindow.xaml.cs文件修改如下:

using DevExpress.Xpf.Core;
using System.Windows;

namespace TEST500ServiceMonitor.Views
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : ThemedWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += OnLoaded;
            for (int i = 0; i <= 100; i++)
            {
                DXSplashScreen.Progress(i);
                DXSplashScreen.SetState(string.Format("{0} %", i));
                System.Threading.Thread.Sleep(40);
            }
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            DXSplashScreen.Close();
        }
    }
}

MainWindow.xaml文件,修改如下:


    
        
            
            
        
        
            
            
        
        
        
            
            
            
            
        
        
            
        
    

MainWindowViewModel文件,修改如下:

using DevExpress.Xpf.Core;
using DevExpress.Xpf.WindowsUI;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System.Windows;
using System.Windows.Input;

namespace TEST500ServiceMonitor.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;

        public MainWindowViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;

            NavigateCommand = new DelegateCommand(Navigate);
            StopServiceCommand = new DelegateCommand(StopService);
            ExitCommand = new DelegateCommand(Exit);
        }
        
        private string _title = "TEST500监控中心";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private string _message = "停止服务";
        public string Message
        {
            get { return _message; }
            set { SetProperty(ref _message, value); }
        }

        public DelegateCommand NavigateCommand { get; private set; }
        public DelegateCommand StopServiceCommand { get; private set; }

        /// 
        /// 关闭程序
        /// 
        public DelegateCommand ExitCommand { get; private set; }

        private void Navigate(string navigatePath)
        {
            if (navigatePath != null)
            {

                _regionManager.RequestNavigate("ContentRegion", navigatePath);
            }
        }

        private void StopService(string s)
        {
            var msg = "";
            if (Message == "启动服务")
                msg = "停止服务";
            else
                msg = "启动服务";
            if (WinUIMessageBox.Show($"确认要{Message}吗?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                Message = msg;
            }
        }

        private void Exit(string win)
        {
            if (WinUIMessageBox.Show("确认要关闭窗口吗?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                Application.Current.Shutdown();
            }
        }
    }
}

模块项目

模块项目中的界面都是创建再UserControl中,只需要再module中注册界面加载方式即可

using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using TEST500ServiceModule.Views;

namespace TEST500ServiceModule
{
    public class TEST500ServiceModuleModule : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            var regionManager = containerProvider.Resolve();
            regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewA));//默认该模块中的默认起始页
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation();//模块中的其他页
        }
    }
}

你可能感兴趣的:(.NET项目实战)