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>();//模块中的其他页
}
}
}