public class MainWindowViewModel : BindableBase
{
private string _title = "Prism Application";
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
readonly IRegionManager regionManager;
IRegionNavigationJournal journal;
public MainWindowViewModel(IRegionManager regionManager)
{
this.regionManager = regionManager;
OpenACommand = new DelegateCommand(OpenA);
OpenBCommand = new DelegateCommand(OpenB);
GoForwardCommand = new DelegateCommand(GoForward);
GoBackCommand = new DelegateCommand(GoBack);
}
public DelegateCommand OpenACommand { get; private set; }
public DelegateCommand OpenBCommand { get; private set; }
public DelegateCommand GoForwardCommand { get; private set; }
public DelegateCommand GoBackCommand { get; private set; }
private void OpenA()
{
NavigationParameters keyValuePairs = new NavigationParameters();
keyValuePairs.Add("value", "Hi A!");
//regionManager.RequestNavigate("ContentRegion", "PageA");//别名导航
//regionManager.RequestNavigate("ContentRegion", "PageA", keyValuePairs);//传递参数
//regionManager.RequestNavigate("ContentRegion", "PageA?value=hello");//url方式传递参数
regionManager.RequestNavigate("ContentRegion", "PageA?value=hello", arg =>
{
journal = arg.Context.NavigationService.Journal;
});
}
private void OpenB()
{
//regionManager.RequestNavigate("ContentRegion", "ViewB");//类名导航
//导航日志
regionManager.RequestNavigate("ContentRegion", "ViewB", arg =>
{
journal = arg.Context.NavigationService.Journal;
});
}
private void GoForward()
{
journal.GoForward();
}
private void GoBack()
{
journal.GoBack();
}
}
public class ViewAViewModel : BindableBase, IConfirmNavigationRequest//INavigationAware
{
private string title;
public string Title
{
get { return title; }
set
{
title = value;
RaisePropertyChanged(nameof(Title));
}
}
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
bool result = true;
if(MessageBox.Show("确认导航?","提示",MessageBoxButton.YesNo)== MessageBoxResult.No)
result = false;
continuationCallback(result);
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
Title = navigationContext.Parameters.GetValue<string>("value");
}
}
//在App.cs中注册
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ViewA>("PageA");//带别名
containerRegistry.RegisterForNavigation<ViewB>();
}