需求:假设WPF 界面设计成经典web后台设计,分几大模块,各个模块独立,这时就需要模块化,prism框架有模块化的设计,解决了这个问题,整体实现方式比较简单。
底部内容栏设计界面:
1、底部内容栏界面设计
<UserControl
x:Class="FooterModule.Views.FooterView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FooterModule.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="36"
d:DesignWidth="800"
mc:Ignorable="d">
<!--<UserControl.Resources>
<ResourceDictionary Source="pack://application:,,,/MotionWindows;component/Styles/Themes/ColorsConfig.xaml" />
</UserControl.Resources>-->
<Grid>
<UniformGrid
Height="36"
Background="#2E2E2E"
Columns="4"
Rows="0">
<Label
Height="36"
Padding="5,0,10,0"
HorizontalAlignment="Left"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Content="{Binding RunTime}"
FontSize="14"
FontWeight="Bold"
Foreground="White" />
<Label
Height="36"
Padding="5,0,10,0"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Content="当前用户:Admin"
FontSize="14"
FontWeight="Bold"
Foreground="White" />
<Label
Height="36"
Padding="5,0,10,0"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Content="智慧平台:2023.01.18"
FontSize="14"
FontWeight="Bold"
Foreground="White" />
<Label
Height="36"
Padding="5,0,10,0"
HorizontalAlignment="Right"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Content="技术支持:xxx"
FontSize="14"
FontWeight="Bold"
Foreground="White" />
</UniformGrid>
</Grid>
</UserControl>
2、ViewModel
using FooterModule.Views;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FooterModule.ViewModels
{
public class FooterViewModel : BindableBase, IModule
{
private string runTime;
public string RunTime { get => runTime; set { runTime = value; RaisePropertyChanged(); } }
public void RegisterTypes(IContainerRegistry containerRegistry)
{
}
/// <summary>
/// 初始化,关联界面
/// </summary>
/// <param name="containerProvider"></param>
public void OnInitialized(IContainerProvider containerProvider)
{
var regionManager = containerProvider.Resolve<IRegionManager>();
regionManager.RegisterViewWithRegion("FooterViewRegionName", typeof(FooterView));
}
public void GetRunTime()
{
Task.Run(new Action(async () =>
{
while (true)
{
await Task.Delay(1000);
var time = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss");
RunTime = "运行时间:" + time;
}
}));
}
public FooterViewModel(IRegionManager regionManager)
{
GetRunTime();
}
}
}
3、模块创建成功后,如何在主程序加载底部模块
第三步:加载模块,prism提供四种加载方式,具体请看官方demo调用方式
//界面上配置一个区域用于加载该模块
<Grid
Grid.Row="2"
Height="60"
Margin="10,0,10,0"
VerticalAlignment="Bottom">
<ContentControl
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
prism:RegionManager.RegionName="FooterViewRegionName" />
</Grid>
配置加载方式
protected override IModuleCatalog CreateModuleCatalog()
{
//路径方式加载模块
return new DirectoryModuleCatalog() { ModulePath = @".\" };
}
这样,如有模块内容有需求变更只需要替换该模块的dll 就可以了
总结:这里只体现了路径加载模块的应用,还有配置文件方式,代码方式等,如需要进一步了解请看官方DEMO