<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>
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(); } } }
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中注册导航视图:
/// <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>(); } }
标签:DelegateCommand,IRegionManager,void,private,using,导航,public From: https://www.cnblogs.com/friend/p/17017901.html