注册
将UserControl通过RegisterForNavigation方法注册为Navigation
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ViewA>();
containerRegistry.RegisterForNavigation<ViewB>();
}
导航视图
通过IRegionManager.RequestNavigate进行视图导航的切换
1、简单导航
IRegionManager _regionManager;
IRegionNavigationService _regionNavigationService;
public NavigationWindowViewModel(IRegionManager regionManager, IRegionNavigationService regionNavigationService)
{
_regionManager = regionManager;
_regionNavigationService = regionNavigationService;
}
private DelegateCommand _viewBCommand;
public DelegateCommand ViewBCommand
{
get
{
if (_viewBCommand == null)
_viewBCommand = new DelegateCommand(() =>
{
// 导航到 ViewB
_regionManager.RequestNavigate("ContentRegion", "ViewB");
});
;
return _viewBCommand;
}
set { _viewBCommand = value; }
}
2、导航传参
- NavigationParameters:导航时传递参数
- Action< NavigationResult >:导航结束时的回调(导航的最后执行)
private DelegateCommand<string> _viewACommand;
public DelegateCommand<string> ViewACommand
{
get
{
if (_viewACommand == null)
_viewACommand = new DelegateCommand<string>((view) =>
{
// 导航到 ViewA
// NavigationParameters 传值
NavigationParameters param = new NavigationParameters();
param.Add("value", "123");
_regionManager.RequestNavigate("ContentRegion", "ViewB",
new Action<NavigationResult>((result) =>
{
}),
param);
});
;
return _viewACommand;
}
set { _viewACommand = value; }
}
INavigationAware
- IsNavigationTarget:控件View是重现(返回True)还是新建(返回False)
- OnNavigatedTo:获取导航传值、导航的日志记录(可前进或者后退)
- OnNavigatedFrom:触发时机不好阐述,最好调试看看
public class ViewAViewModel : BindableBase, INavigationAware, IConfirmNavigationRequest
{
/// <summary>
/// </summary>
/// <param name="navigationContext"></param>
public void OnNavigatedTo(NavigationContext navigationContext)
{
// 导航传值
var param = navigationContext.Parameters["value"];
// 历史记录
_journal = navigationContext.NavigationService.Journal;
//_journal.GoForward();
//_journal.GoBack();
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
// 控件View是重现(返回True)还是新建(返回False)
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
// navigationContext可以拿ViewA和ViewB两个对象
// 触发时机
}
}
IConfirmNavigationRequest
导航某个页面,添加判断,是否是可以导航过去
public class ViewAViewModel : BindableBase, INavigationAware, IConfirmNavigationRequest
{
// ===============================================IConfirmNavigationRequest
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
bool result = false;
if (MessageBox.Show("数据没保存,是否离开", "确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
// 允许导航
result = true;
}
// 判断是否允许导航
continuationCallback(result);
}
}
IRegionNavigationService和IRegionNavigationJournal
_regionManager.Regions[“ContentRegion”].NavigationService可以获得IRegionNavigationService对象;
IRegionNavigationService.Journal可以获得IRegionNavigationJournal对象;
private DelegateCommand _forwordCommand;
public DelegateCommand ForwordCommand
{
get
{
if (_forwordCommand == null)
_forwordCommand = new DelegateCommand(() =>
{
var j = _regionManager.Regions["ContentRegion"].NavigationService.Journal;
if (j.CanGoForward)
j.GoForward();
});
return _forwordCommand;
}
set { _forwordCommand = value; }
}
private DelegateCommand _backCommand;
public DelegateCommand BackCommand
{
get
{
if (_backCommand == null)
_backCommand = new DelegateCommand(() =>
{
//var j = _regionNavigationService.Journal;
var j = _regionManager.Regions["ContentRegion"].NavigationService.Journal;
if (j.CanGoBack)
j.GoBack();
});
return _backCommand;
}
set { _backCommand = value; }
}