WPF Prism 7.0+ INavigationAware(导航)

示例:向模块发送参数

方式一、无参数导航

regionManager.RequestNavigate("LoginRegion", "LoginControl");

 方式二、带参数导航

NavigationParameters pairs = new NavigationParameters();
pairs.Add("参数名", "参数值");
regionManager.Regions["LoginRegion"].RequestNavigate(obj, pairs);

方式三、带参数导航,并且有回调通知

NavigationParameters pairs = new NavigationParameters();
pairs.Add("参数名", "参数值");
regionManager.Regions["LoginRegion"].RequestNavigate(obj, navigationCallback =>
{
    if (navigationCallback.Result == true)
    {
        var journal = navigationCallback.Context.NavigationService.Journal;
    }
}, pairs);

 

示例:模块获取参数,实现 INavigationAware 接口

public class LoginViewModel : BindableBase, INavigationAware
{
    #region 参数处理
    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
        throw new NotFiniteNumberException();
    }
    /// <summary>
    /// 获取参数
    /// </summary>
    public void OnNavigatedTo(NavigationContext navigationContext)
    {
        if (navigationContext.Parameters.ContainsKey("参数名"))
        {
            string Content = navigationContext.Parameters.GetValue<string>("参数名");
        }
    }
    public void OnNavigatedFrom(NavigationContext navigationContext)
    {

    }
    /// <summary>
    /// 模块切换时触发,回调
    /// </summary>
    public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
    {
        bool result = true;
        if (MessageBox.Show("确认是否要离开当前模块?", "系统提示", MessageBoxButton.YesNo) == MessageBoxResult.No)
        {
            result = false;
        }
        continuationCallback(result);
    }
    #endregion
}

 

posted @ 2024-07-02 17:51  microsoft-zhcn  阅读(58)  评论(0编辑  收藏  举报