本次简单记录一下Prism中区域导航功能的使用
第一步 准备导航页面
新建两个UserControl (RegionFirstView.xaml ,RegionSecondView.xaml),作为导航的目标页面
简单在两个页面上做好标记
RegionFirstView.xaml
<Grid> <Border Background="Green"/> <TextBlock Text="RegionFirstView" VerticalAlignment="Center" HorizontalAlignment="Center"/> <TextBox VerticalAlignment="Bottom"/> </Grid>
RegionSecondView.xaml
<Grid> <Border Background="Red"/> <TextBlock Text="RegionSecondView" VerticalAlignment="Center" HorizontalAlignment="Center"/> </Grid>
第二步 依赖注入
将这两个页面注入到容器中,打开App.xmal.cs
public partial class App : PrismApplication { protected override Window CreateShell() { return Container.Resolve<RegionManageView>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<RegionFirstView>(); containerRegistry.RegisterForNavigation<RegionSecondView>(); } }
第三步 主窗体布局
注意:ViewModelLocator
类具有附加属性, AutoWireViewModel
该属性用于将视图模型与视图相关联。 在视图的 XAML 中,此附加属性设置为 true 以指示视图模型应自动连接到视图
用于显示导航内容的 <ContentControl Grid.Column="1" prism:RegionManager.RegionName="MainContent"/>,为其区域起名为"MainContent"
<Window x:Class="MvvmBase.PrismDemo.Views.RegionManageView" 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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MvvmBase.PrismDemo.Views" mc:Ignorable="d" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" Title="RegionManageView" Height="450" Width="800"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <StackPanel Width="150"> <Button Height="25" Content="OpenRegionFirstView" Command="{Binding OpenRegionCommand}" CommandParameter="RegionFirstView"/> <Button Height="25" Content="OpenRegionSecondView" Command="{Binding OpenRegionCommand}" CommandParameter="RegionSecondView"/> <Button Height="25" Content="CloseRegionFirstView" Command="{Binding CloseRegionCommand}" CommandParameter="RegionFirstView"/> <Button Height="25" Content="CloseRegionSecondView" Command="{Binding CloseRegionCommand}" CommandParameter="RegionSecondView"/> </StackPanel> <ContentControl Grid.Column="1" prism:RegionManager.RegionName="MainContent"/> </Grid> </Window>
第四步 RegionManageViewModel 类实现
通过构造函数,容器将区域管理类注入进来,从而获取到 IRegionManager,可用于管理导航类
4.1 实现打开和关闭命令
1)点击打开或关闭时,由于前端会通过CommandParameter将需要操作的页面传递到方法中,也就是 方法DoCloseRegionCommand(string obj)和DoOpenRegionCommand(string obj)的参数obj
2)当执行打开命令OpenRegionCommand时,将会为区域MainContent 添加导航页面 ;假如传入obj为RegionFirstView,区域管理器将会从容器中寻找名字为RegionFirstView页面,并导航到该页面
3)当执行打开命令CloseRegionCommand时,
var views = regionManager.Regions["MainContent"]; // 获取区域 MainContent的所有视图
var view = views.Views.FirstOrDefault(it => it.GetType().Name == obj);//找到需要关闭的页面、
views.Remove(view);// 将该页面移除
执行时,
public class RegionManageViewModel:BindableBase { private readonly IRegionManager regionManager; public DelegateCommand<string> OpenRegionCommand { get; private set; } public DelegateCommand<string> CloseRegionCommand { get; private set; } public RegionManageViewModel(IRegionManager regionManager) { OpenRegionCommand = new DelegateCommand<string>(DoOpenRegionCommand); CloseRegionCommand = new DelegateCommand<string>(DoCloseRegionCommand); this.regionManager = regionManager; } private void DoCloseRegionCommand(string obj) { var views = regionManager.Regions["MainContent"]; var view = views.Views.FirstOrDefault(it => it.GetType().Name == obj); if(view!=default) { views.Remove(view); } } private void DoOpenRegionCommand(string obj) { regionManager.Regions["MainContent"].RequestNavigate(obj); } }
第五步 演示
5.1 启动
5.2 打开RegionFirstView和RegionSecondView
5.3 点击移除 打开的导航
标签:obj,MainContent,MVVM,regionManager,视图,Prism,RegionManager,导航,页面 From: https://www.cnblogs.com/just-like/p/17128496.html