Prism 基础知识学习(三)导航功能
Prism 基础知识学习(三)导航功能
-
自动匹配(AutoWireViewModel)
在View文件中加入下面代码,让View与对应的ViewModel自动绑定
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
-
手动设置。在Module的配置文件中修改RegisterTypes对应关系。
public class ModuleAModule : IModule { public void OnInitialized(IContainerProvider containerProvider) { } public void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<ViewA, ViewAViewModel>(); } }
-
导航字典功能。
在MainViewModels 中添加导航参数,然后在ViewA中进行接收
MainWindowViewModel.cs
private void Open(string obj) { NavigationParameters keys = new NavigationParameters(); keys.Add("Title", "Hello!"); regionManager.Regions["ContentRegion"].RequestNavigate(obj, keys); }
然后在ViewAViewModel.cs
public class ViewAViewModel :BindableBase, IConfirmNavigationRequest
{
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
public ViewAViewModel()
{
}
/// <summary>
/// 每次重新导航的时候,是否重用原来的实例
/// </summary>
/// <param name="navigationContext"></param>
/// <returns></returns>
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
Title= navigationContext.Parameters.GetValue<string>("Title");
}
/// <summary>
/// 是否允许切换到下一个界面
/// </summary>
/// <param name="navigationContext"></param>
/// <param name="continuationCallback"></param>
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
bool result = true;
if (MessageBox.Show("确认导航?", "温馨提示", MessageBoxButton.YesNo) == MessageBoxResult.No)
result = false;
continuationCallback(result);
}