导航 IRegionManager

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <Window x:Class= "PrismWpfBlankApp.Views.MainWindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism= "http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel= "True" Title= "{Binding Title}" Height= "350" Width= "525" > <Grid> <Grid.RowDefinitions> <RowDefinition Height= "Auto" ></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <StackPanel Margin= "10" Orientation= "Horizontal" > <Button Margin= "10" Command= "{Binding GoBackCommand}" Content= "上一页" ></Button> <Button Margin= "10" Command= "{Binding GoForwardCommand}" Content= "下一页" ></Button> <Button Margin= "10" Command= "{Binding OpenACommand}" Content= "打开A" ></Button> <Button Margin= "10" Command= "{Binding OpenBCommand}" Content= "打开B" ></Button> </StackPanel> <ContentControl Grid.Row= "1" prism:RegionManager.RegionName= "ContentRegion" /> </Grid> </Window> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | using DryIoc; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using PrismWpfBlankApp.Views; using System.Net.NetworkInformation; namespace PrismWpfBlankApp.ViewModels { public class MainWindowViewModel : BindableBase { private readonly IRegionManager m_RegionManager; private IRegionNavigationJournal m_Journal; //导航日志 private string _title = "Prism Application" ; public string Title { get { return _title; } set { SetProperty( ref _title, value); } } public MainWindowViewModel(IRegionManager regionManager) { m_RegionManager = regionManager; GoBackCommand = new DelegateCommand(GoBack); GoForwardCommand = new DelegateCommand(GoForward); OpenACommand = new DelegateCommand(OpenA); OpenBCommand = new DelegateCommand(OPenB); } public DelegateCommand GoBackCommand { get ; } public DelegateCommand GoForwardCommand { get ; } public DelegateCommand OpenACommand { get ; } public DelegateCommand OpenBCommand { get ; } private void OpenA() { //导航到 ViewA 视图并传递参数 NavigationParameters param = new NavigationParameters(); param.Add( "Value" , "Hello" ); m_RegionManager.RequestNavigate( "ContentRegion" , "PageA" , arg => { m_Journal = arg.Context.NavigationService.Journal; }, param); //或者使用 Http 方式,传递参数 //m_RegionManager.RequestNavigate("ContentRegion", "PageA?Value=Hello 来自查询参数"); } private void OPenB() { //导航到 ViewB 视图 m_RegionManager.RequestNavigate( "ContentRegion" , nameof(ViewB), arg => { m_Journal = arg.Context.NavigationService.Journal; }); } private void GoBack() { m_Journal.GoBack(); } private void GoForward() { m_Journal.GoForward(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace PrismWpfBlankApp.ViewModels { public class ViewAViewModel : BindableBase, IConfirmNavigationRequest { //INavigationAware 用于导航传递参数 //IConfirmNavigationRequest 导航离开前确认,IConfirmNavigationRequest 继承INavigationAware private string message; public string Message { get { return message; } set { SetProperty( ref message, value); } } #region 接口实现 public void ConfirmNavigationRequest(NavigationContext navigationContext, Action< bool > continuationCallback) { //确认导航请求,离开当前视图前弹出提示框,以便保存未保存的数据 bool result = true ; if (MessageBox.Show( "是否要离开ViewA" , "提示" , MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK) { result = false ; } continuationCallback?.Invoke(result); } /// <summary> /// 是否重新创建导航实体 /// </summary> /// <param name="navigationContext"></param> /// <returns></returns> public bool IsNavigationTarget(NavigationContext navigationContext) { return true ; } /// <summary> /// 导航离开当前页时触发 /// </summary> /// <param name="navigationContext"></param> public void OnNavigatedFrom(NavigationContext navigationContext) { //旧视图 } /// <summary> /// 导航完成前,接收用户传递的参数以及是否允许导航等控制 /// </summary> /// <param name="navigationContext"></param> public void OnNavigatedTo(NavigationContext navigationContext) { //新视图 var value = navigationContext.Parameters.GetValue< string >( "Value" ); if ( string .IsNullOrEmpty(value) == false ) { Message = value.ToString(); } } #endregion } } |
在App中注册导航视图:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App { protected override Window CreateShell() { return Container.Resolve<MainWindow>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<ViewA>( "PageA" ); containerRegistry.RegisterForNavigation<ViewB>(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)