一、页面跳转概述 在Prism中,使用Navigation来进行页面之间的跳转通常需要一下几步:
- 创建新页面,并且实现INavigationAware接口
- 使用IRegionManager注册页面
- 使用NavigationParameters封装页面跳转的参数
- 使用IRegionManager.RequestNavigate()跳转到目标页面 一、页面跳转概述 在Prism中,使用Navigation来进行页面之间的跳转通常需要一下几步:
- 创建新页面,并且实现INavigationAware接口
- 使用IRegionManager注册页面
- 使用NavigationParameters封装页面跳转的参数
- 使用IRegionManager.RequestNavigate()跳转到目标页面
二、例子 创建新页面,并且实现INavigationAware接口 public partial class ContactMainWindowView : UserControl, INavigationAware { public ContactMainWindowView() { InitializeComponent();
}
#region INavigationAware
public void OnNavigatedTo(NavigationContext navigationContext)
{
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
var param = navigationContext.Parameters["Param"] ;
Console.WriteLine(param?.ToString());
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
#endregion
}
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 使用IRegionManager注册页面
ServiceLocator.Current.GetInstance() .Regions[RegionNames.ContentRegion].Add(new ContactMainWindowView()); 1 2 3 4 用NavigationParameters封装参数: 方法一:该方法参数类型可以为object
var para = new NavigationParameters();
para.Add("Uri", "ContactMainWindowView");//此处建议将ContactMainWindowView定义为常量
para.Add("Param", "hello saylor");//key必须为字符串,value可以传递object类型,此处用"hello saylor"
1 2 3 方法二:该方法参数类型可以为string
var para = new NavigationParameters("Uri=ContactMainWindowView&Param=hello saylor");
1 2 使用IRegionManager.RequestNavigate()跳转到目标页面 ServiceLocator.Current.GetInstance() .RequestNavigate(RegionNames.ContentRegion, para[“Uri”].ToString(), para); 1 2 3 三、总结 使用IRegionManager注册页面时,是new一个新页面(不要多次注册)。并且对应的region会默认显示第一个注册的View。 使用了ServiceLocator来获取IRegionManager的实例。prism中,默认创建了实例,并进行了注册(依赖注入)。 用NavigationParameters封装参数,方法二中,构造函数的参数类似于get请求中的参数形式。