Wpf使用Prism的IRegionManager实现页面导航
背景
之前使用winform的形式,利用事件和委托(复杂可以利用反射)实现了wpf的页面跳转。
页面间的导航可以通过使用Prism类库实现,本章节主要讲述这个。
参考内容
文字讲解因为有人做了更好的讲解,所以直接将索引放在下方。但单凭讲解没有完整代码,理解需要更多的时间。特将demo的全部讲解贴在此处。
讲解:Prism区域管理实现导航_regionmanager.registerviewwithregion("lefttreemenu-CSDN博客
实际代码
App.xaml
<prism:PrismApplication x:Class="WPFPrism.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFPrism"
xmlns:prism="http://prismlibrary.com/">
<Application.Resources>
</Application.Resources>
</prism:PrismApplication>
App.xaml.cs
using ModuleA;
//using ModuleB;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using System.Windows;
using WPFPrism.Views;
namespace WPFPrism
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
/// <summary>
/// Creates the shell or main window of the application.
/// </summary>
/// <returns></returns>
protected override Window CreateShell()
{
//通过容器去拿MainView,也是启动的地方
return Container.Resolve<MainView>();
}
/// <summary>
/// Used to register types with the container that will be used by your application.
/// </summary>
/// <param name="containerRegistry"></param>
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//依赖注入的功能,增加依赖注入要实现的内容
//containerRegistry.RegisterForNavigation<ViewA>(); //containerRegistry.RegisterForNavigation<ViewA>("ViewA的具体名字")自定义,可以这样写;
//containerRegistry.RegisterForNavigation<ViewB>();
containerRegistry.RegisterForNavigation<ViewC>();//我们依然使用C模块,C模块是当前程序的。
containerRegistry.RegisterForNavigation<ViewD>();
}
/// <summary>
/// Configures the Prism.Modularity.IModuleCatalog used by Prism.
/// </summary>
/// <param name="moduleCatalog"></param>
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
//这里是走代码的
//moduleCatalog.AddModule<ModuleAfile>();//加载A模块
moduleCatalog.AddModule<ModuleBfile>();//加载B模块
base.ConfigureModuleCatalog(moduleCatalog);
}
/// <summary>
/// Creates the Prism.Modularity.IModuleCatalog used by Prism.
/// </summary>
/// <returns></returns>
protected override IModuleCatalog CreateModuleCatalog()
{
//这里是走配置的
//return new DirectoryModuleCatalog()
//{
// ModulePath = @"Modules"//这里是主程序exe同文件夹下面的Modules文件夹
//};
return base.CreateModuleCatalog();
}
}
}
MainViewModel.xaml.cs
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFPrism.Views;
namespace WPFPrism.ViewModels
{
public class MainViewModel : BindableBase
{
private readonly IRegionManager regionManager;//区域管理
private IRegionNavigationJournal journal;//区域导航日志
public DelegateCommand<string> OpenCommand { get; set; }//创建命令,string用来接收参数,例如ViewA
public DelegateCommand BackCommand { get; set; }//创建导航上一步命令
public DelegateCommand ForwardCommand { get; set; }//创建导航下一步命令
public MainViewModel(IRegionManager regionManager)//这里点击快速操作。拿界面定义的区域Content
{
BackCommand = new DelegateCommand(Back);
ForwardCommand = new DelegateCommand(Forward);
OpenCommand = new DelegateCommand<string>(Open);
this.regionManager = regionManager;//拿到这个值
}
private void Forward()
{
if (journal.CanGoForward)
journal.GoForward();
}
private void Back()//返回上一步方法
{
if (journal.CanGoBack)
journal.GoBack();
}
private void Open(string obj)
{
NavigationParameters keys = new NavigationParameters();
keys.Add("Title", "Hello");
regionManager.Regions["Content"].RequestNavigate(obj, callBack =>
{
if ((bool)callBack.Result)//导航问题
{
journal = callBack.Context.NavigationService.Journal;
}
}, keys);//这里需要依赖注入的方式
}
}
}
MainView.xaml
<Window x:Class="WPFPrism.Views.MainView"
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:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
xmlns:local="clr-namespace:WPFPrism"
mc:Ignorable="d"
Title="MainView" Height="450" Width="800">
<!--自动关联AutoWireViewModel-->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" >
<Button Content="打开模块A" Margin="5" Command="{Binding OpenCommand}" CommandParameter="ViewA"/>
<Button Content="打开模块B" Margin="5" Command="{Binding OpenCommand}" CommandParameter="ViewB"/>
<Button Content="打开模块C" Margin="5" Command="{Binding OpenCommand}" CommandParameter="ViewC"/>
<Button Content="打开模块D" Margin="5" Command="{Binding OpenCommand}" CommandParameter="ViewD"/>
<Button Content="返回上一步" Margin="5" Command="{Binding BackCommand}" />
<Button Content="返回下一步" Margin="5" Command="{Binding ForwardCommand}" />
</StackPanel>
<ContentControl Grid.Row="1" prism:RegionManager.RegionName="Content" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFPrism.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainView : Window
{
public MainView()
{
InitializeComponent();
}
}
}
ViewC.xaml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFPrism.Views
{
/// <summary>
/// ViewC.xaml 的交互逻辑
/// </summary>
public partial class ViewC : UserControl
{
public ViewC()
{
InitializeComponent();
}
}
}
<UserControl x:Class="WPFPrism.Views.ViewC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPFPrism.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" Background="Orange">
<Grid>
<TextBlock Text="我 是 C" FontSize="50"/>
</Grid>
</UserControl>
ViewD.xaml
<UserControl x:Class="WPFPrism.Views.ViewD"
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:WPFPrism.Views"
mc:Ignorable="d" Height="450" Width="800" Background="Green">
<Grid>
<TextBlock Text="我 是 D" FontSize="50"/>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WPFPrism.Views
{
/// <summary>
/// ViewD.xaml 的交互逻辑
/// </summary>
public partial class ViewD : UserControl
{
public ViewD()
{
InitializeComponent();
}
}
}
ViewAViewModel.cs
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace ModuleA.ViewModels
{
public class ViewAViewModel : BindableBase, IConfirmNavigationRequest //INavigationAware //IConfirmNavigationRequest导航离开的时候,进行判断提示
{
private string title;
public string Title
{
get { return title; }
set { title = value; RaisePropertyChanged(); }
}
public ViewAViewModel()
{
//执行顺序1
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
//每次重新重新导航的时候,是否重用原来的实例
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
//在导航离开时调用。
}
/// <summary>
/// 当已导航到实现时调用。
/// </summary>
/// <param name="navigationContext"></param>
public void OnNavigatedTo(NavigationContext navigationContext)
{
//执行顺序2
if (navigationContext.Parameters.ContainsKey("Title"))//判断是否有值再赋值
{
Title = navigationContext.Parameters.GetValue<string>("Title");
}
}
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
//导航离开的时候,进行判断提示,是否允许从A到B模块
bool b = true;
if (MessageBox.Show("确定切换吗", "提示", MessageBoxButton.YesNo) == MessageBoxResult.No)
b = false;
continuationCallback(b);
}
}
}
ViewA.xaml
<UserControl x:Class="ModuleA.Views.ViewA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ModuleA.Views"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" Background="Red">
<Grid>
<TextBlock Text="我是A" FontSize=" 30"/>
<TextBlock Margin="50" Text="{Binding Title}" FontSize=" 30"/>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ModuleA.Views
{
/// <summary>
/// ViewsA.xaml 的交互逻辑
/// </summary>
public partial class ViewA : UserControl
{
public ViewA()
{
InitializeComponent();
}
}
}
ModuleAfile.cs
using ModuleA.ViewModels;
using ModuleA.Views;
using Prism.Ioc;
using Prism.Modularity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModuleA
{
public class ModuleAfile : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
//初始化
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
//依赖注入,模块注册。和主程序继承PrismApplication类似。
containerRegistry.RegisterForNavigation<ViewA,ViewAViewModel>();//进行匹配,这里一般不使用自动匹配
}
}
}
好了,分享就到这里。
标签:IRegionManager,xaml,System,Windows,Prism,using,Wpf,public From: https://blog.csdn.net/yannsann/article/details/140423206