当前位置: 首页 > article >正文

WPF区域导航+导航参数使用+路由守卫+导航日志

背景:使用ContentControl控件实现区域导航是有Mvvm框架的WPF都能使用的,不限于Prism

主要是将ContenControl控件的Content内容在ViewModel中切换成不同的用户控件

下面是MainViewModel:

private object body;

public object Body
{
    get { return body; }
    set { body = value; RaisePropertyChanged(); }
}


public DelegateCommand<string> OpenCommand { get; set; }

public MainWindowViewModel()
{
    OpenCommand = new DelegateCommand<string>(obj =>
    {
        Body = obj switch
        {
            "ViewA" => new ViewA(),
            "ViewB" => new ViewB(),
            "ViewC" => new ViewC(),
            _ => Body
        };
    });
}


上面是有Mvvm框架就行了,每次打开新的模块就创建一个用户控件对象

下面是使用Prism框架的导航实现会方便一些

1.首先在App.xaml.cs中注入用户控件的依赖

2.ContentControl中的Content修改为:

<ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion" />

3.MainWindowViewModel变成:

public class MainWindowViewModel : BindableBase
{
    private readonly IRegionManager regionManager;

    public DelegateCommand<string> OpenCommand { get; set; }

    public MainWindowViewModel(IRegionManager regionManager)
    {
        OpenCommand = new DelegateCommand<string>(obj => { regionManager.Regions["ContentRegion"].RequestNavigate(obj); });
        this.regionManager = regionManager;
    }
}

-- 也就是由创建用户控件,变成调用依赖注入的用户控件

导航参数

在调用导航前设置导航参数,请求导航的时候将导航参数传递过去

NavigationParameters keys = new NavigationParameters();
keys.Add("Title", "Hello");

regionManager.Regions["ContentRegion"].RequestNavigate(viewName, keys);

然后导航用户控件的ViewModel需要接口INavigationAware,接口重写方法中都是带有参数NavigationContext的,然后通过它获取导航参数就行

public void OnNavigatedTo(NavigationContext navigationContext)
{
    if (navigationContext.Parameters.ContainsKey("Title"))
        Tile = navigationContext.Parameters.GetValue<string>("Title");
}

路由守卫

需要实现路由守卫需要将原本的INavigationAware接口换成IConfirmNavigationRequest,重写的方法如果continuationCallback的值是true就给导航过去,如果没有就不给导航

public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
    bool result = true;

    if (MessageBox.Show("请求导航?", "温馨提示", MessageBoxButton.YesNo) == MessageBoxResult.No)
    {
        result = false;
    }

    continuationCallback(result);
}

导航日志

private IRegionNavigationJournal journal;
// 打开区域的方法
private void OpenView(string viewName)
{
    NavigationParameters keys = new NavigationParameters();
    keys.Add("Title", "Hello");

    // 调用完区域就记录在日志中
    regionManager.Regions["ContentRegion"].RequestNavigate(viewName, callBack =>
    {
        if (callBack.Cancelled)
        {
            return;
        }
        journal = callBack.Context.NavigationService.Journal;
    }, keys);
}

//返回上一页方法(让按钮绑定就行)
private void back()
{
    if (journal.CanGoBack)
        journal.GoBack();
}


http://www.kler.cn/a/469304.html

相关文章:

  • axios和fetch的实现原理以及区别,与XMLHttpRequest的关系,并结合react封装统一请求示例
  • 安装Linux
  • 【C++】B2091 向量点积计算
  • 【stm32+K210项目】基于K210与STM32协同工作的智能垃圾分类系统设计与实现(完整工程资料源码)
  • JavaWeb开发(六)XML介绍
  • 使用WebSocket 获取实时数据
  • 06-C++类和对象强化
  • UDP接收和断线重连代码注入案例
  • vue使用树形结构展示文件和文件夹
  • 港大发布OpenCity: 大模型驱动下的智慧城市“新内核“
  • 关于python的数据分析与应用
  • Linux上vi(vim)编辑器使用教程
  • MYSQL---------支持数据类型
  • unity中的UI系统---GUI
  • 如何使用Termux 通过 SSH 连接到远程服务器
  • vue3 如何封装aixos
  • 【AI数学基础】线性代数:内积和范数
  • 换肤-主题:使用 CSS 自定义属性 -- var()函数 详解
  • 报错:websocket注入为null,已解决!
  • 在 uni-app 中使用 wxml-to-canvas 的踩坑经验总结