WPF Prism构建模块式开发框架

Prism框架扩展安装

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

资源项目

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

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
                    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors">
    <Style x:Key="Button" TargetType="dx:SimpleButton">
        <Setter Property="Foreground" Value="AliceBlue"/>
        <Setter Property="Width" Value="70"/>
        <Setter Property="Height" Value="50"/>
    </Style>
</ResourceDictionary>

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
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        public App()
        {
        //使用主题(devexpress),
            ApplicationThemeHelper.ApplicationThemeName = Theme.Office2016ColorfulName;
            ApplicationThemeHelper.UseLegacyDefaultTheme = false;
            //启动splash
            DXSplashScreen.Show<SplashView>();
        }

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

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }

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

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

<prism:PrismApplication x:Class="TEST500ServiceMonitor.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:TEST500ServiceMonitor">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/TEST500ServiceStyle;component/DefaultStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</prism:PrismApplication>

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

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

namespace TEST500ServiceMonitor.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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文件,修改如下:

<dxc:ThemedWindow
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
        xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core"
        xmlns:TEST500ServiceMonitor="clr-namespace:TEST500ServiceMonitor"
        x:Class="TEST500ServiceMonitor.Views.MainWindow"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" WindowStartupLocation="CenterScreen" Height="400" Width="600" ResizeMode="NoResize">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="260"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Image Source="pack://application:,,,/TEST500ServiceMonitor;component/Images/TEST500Logo.png" Margin="10 10 10 10"/>
        <StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="0">
            <dxc:SimpleButton Command="{Binding StopServiceCommand}" CommandParameter="ViewA" Margin="5" Content="{Binding Message}" Style="{StaticResource Button}"/>
            <dxc:SimpleButton Command="{Binding NavigateCommand}" CommandParameter="ViewA" Margin="5" Content="在线用户" Style="{StaticResource Button}"/>
            <dxc:SimpleButton Command="{Binding NavigateCommand}" CommandParameter="Test500History" Margin="5" Content="历史记录" Style="{StaticResource Button}"/>
            <dxc:SimpleButton Command="{Binding ExitCommand}" CommandParameter="ViewA" Margin="5" Content="退出程序" Style="{StaticResource Button}"/>
        </StackPanel>
        <dxc:LoadingDecorator Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" BorderEffect="Default" BorderEffectColor="Blue" UseFadeEffect="True">
            <ContentControl prism:RegionManager.RegionName="ContentRegion" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" />
        </dxc:LoadingDecorator>
    </Grid>
</dxc:ThemedWindow>

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<string>(Navigate);
            StopServiceCommand = new DelegateCommand<string>(StopService);
            ExitCommand = new DelegateCommand<string>(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<string> NavigateCommand { get; private set; }
        public DelegateCommand<string> StopServiceCommand { get; private set; }

        /// <summary>
        /// 关闭程序
        /// </summary>
        public DelegateCommand<string> 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<IRegionManager>();
            regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewA));//默认该模块中的默认起始页
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<Test500History>();//模块中的其他页
        }
    }
}
  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: WPF Prism框架是一个面向对象的框架,用于开发模块化、可扩展的WPF应用程序,它基于MVVM设计模和依赖注入技术。该框架的主要目的是能够轻松地实现可插拔的模块,公共的服务、组件和工具类的共享,同时也提供了灵活的路由、事件聚合、模块加载、导航和命令处理等机制。使用WPF Prism框架可以快速地开发出灵活的WPF应用程序,从而提高代码质量和开发效率,减少代码的维护难度。 WPF Prism框架具有以下的特点: 1. 扩展性:可以轻松地添加新的模块、服务和组件,同时也可以快速替换现有的组件。 2. 可重用性:提供了丰富的公共组件、服务和工具类,从而可以提高代码的可重用性。 3. 灵活性:提供了灵活的路由、事件聚合、模块加载和导航等机制,能够更好地满足应用程序的需求。 4. 易用性:提供了一套完整的MVVM设计模和依赖注入技术的实践方案,从而能够更好地组织应用程序的逻辑。 总之,WPF Prism框架是一个强大的工具,能够让我们更好地开发WPF应用程序,提高代码质量和开发效率,实现可插拔的模块化和可扩展性,同时也具有灵活性和易用性。 ### 回答2: WPF Prism框架是一种面向MVVM模的开源框架,它帮助开发人员使用模块化的方构建可扩展、可重用和易于维护的WPF应用程序。该框架主要由Microsoft和模仲裁者团队开发和维护,它借鉴了许多现代的软件开发理念,比如IoC容器、依赖注入和事件聚合器等。 WPF Prism框架的核心思想是将应用程序分解为许多可独立维护和扩展的模块。这些模块可以基于业务逻辑、UI、数据或任何其他特征进行分组。在该框架中,模块由各种名为“组件”的构建块组成。这些组件包括视图(View)、视图模型(ViewModel)、服务(Service)、模型(Model)等。通过基于这些组件的开发,可以实现具有高度可伸缩性和可重用性的应用程序。 为了实现这种模块开发和组件化架构,WPF Prism框架提供了一些重要的工具和特性。例如,在该框架中可以使用依赖注入容器(如Unity)来管理组件及其依赖关系。此外,该框架还提供了一些基于事件的消息机制,可方便地实现模块间的交互和通信。 总体来说,WPF Prism框架是一种利用开源技术实现企业级应用程序开发的最佳选择。它具有良好的模块化、组件化和可扩展性特性,可以在实现复杂WPF应用程序时提高开发效率和代码质量。 ### 回答3: WPF Prism是一个基于WPF框架,它为大型应用程序提供了一种组织、设计和部署的方。它旨在帮助开发构建可扩展、可维护和可测试的WPF应用程序。 WPF Prism采用了面向模块的编程思想,它将整个应用程序划分为多个模块,每个模块都包含自己的逻辑和用户界面。这种模块化设计使得应用程序易于扩展和维护,同时也简化了开发流程。 WPF Prism同时提供了一组强大的工具和功能,如依赖注入、命令模和事件聚合等,这些功能让WPF应用程序更加易于开发和测试。它还提供了一个强大的导航和区域管理系统,开发者可以使用这些系统来管理不同部分的用户界面和功能。 总之,WPF Prism是一个优秀的框架,它为开发者提供了全面的工具和功能,使得构建WPF应用程序变得更加容易和高效。它的结构良好、可扩展性强,而且可以充分利用WPF的强大功能。无论是大型企业应用程序还是小型桌面应用程序,WPF Prism都是一个理想的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值