Wpf 使用 Prism 实战开发Day21

news/2024/9/25 6:18:39/

配置默认首页

当应用程序启动时,默认显示首页

一.实现思路,通过自定义接口来配置应用程序加载完成时,设置默认显示页

步骤1.创建自定义 IConfigureService 接口

namespace MyToDo.Common
{/// <summary>/// 配置默认显示页接口/// </summary>public interface IConfigureService{void Configure();}
}

步骤2.MainViewModel 去继承并实现该接口。同时把初始化菜单操作,以及配置默认启动页操作在该方法中去处理。

namespace MyToDo.ViewModels
{public class MainViewModel: BindableBase, IConfigureService{public MainViewModel(IRegionManager regionManager){MenuBars=new ObservableCollection<MenuBar>();NavigateCommand = new DelegateCommand<MenuBar>(Navigate);this.regionManager = regionManager;GoBackCommand = new DelegateCommand(() =>{if(journal!=null && journal.CanGoBack) journal.GoBack();});GoForwardCommand = new DelegateCommand(() =>{if (journal != null && journal.CanGoForward) journal.GoForward();});}/// <summary>/// 导航方法/// </summary>/// <param name="bar">菜单</param>private void Navigate(MenuBar bar){//命名空间为空,不进行导航if (bar == null || string.IsNullOrEmpty(bar.NameSpace)) return;regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(bar.NameSpace, back =>{journal=back.Context.NavigationService.Journal;      }); }/// <summary>/// 导航命令/// </summary>public DelegateCommand<MenuBar> NavigateCommand { get; private set; }/// <summary>/// 下一步/// </summary>public DelegateCommand GoBackCommand { get; private set; }/// <summary>/// 上一步/// </summary>public DelegateCommand GoForwardCommand { get; private set; }private ObservableCollection<MenuBar> menuBars;private readonly IRegionManager regionManager;/// <summary>/// 导航日志/// </summary>private IRegionNavigationJournal journal;public ObservableCollection<MenuBar> MenuBars{get { return menuBars; }set { menuBars = value; RaisePropertyChanged(); }}void CreateMenuBar(){MenuBars.Add(new MenuBar() { Icon="Home",Title="首页",NameSpace="IndexView"});MenuBars.Add(new MenuBar() { Icon = "NotebookCheckOutline", Title = "待办事项", NameSpace = "ToDoView" });MenuBars.Add(new MenuBar() { Icon = "NotebookPlusOutline", Title = "忘备录", NameSpace = "MemoView" });MenuBars.Add(new MenuBar() { Icon = "Cog", Title = "设置", NameSpace = "SettingsView" });}public void Configure(){CreateMenuBar();regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate("IndexView");}}
}
  • RequestNavigate 里面直接填写字符串就能该到该视图,是因为所有的视图都已在 App中进行依赖注入了。所以Prism能直接通过对应的字符串找到对应的视图。

步骤3.在 App中,当应用程序主窗口加载完成的时候,通过重写OnInitialized 方法,在该方法中通过拿到主窗口实例,去调用到自定义的方法来实现加载设置的默认首页。

       protected override void OnInitialized(){var service= App.Current.MainWindow.DataContext as IConfigureService;if (service != null) service.Configure();base.OnInitialized();}


http://www.ppmy.cn/news/1429098.html

相关文章

Docker - 镜像、容器、仓库

原文地址&#xff0c;使用效果更佳&#xff01; Docker - 镜像、容器、仓库 | CoderMast编程桅杆Docker - 镜像、容器、仓库 提示 这个章节涉及到 Docker 最核心的知识&#xff0c;也是在使用过程中最常使用到的&#xff0c;需要重点学习。 什么是Docker镜像、容器、仓库&…

C语言语法进阶

条件运算符 条件运算符是 C 语言中唯一的一种三目运算符。三目运算符代表有三个操作数&#xff1b;双目 运算符代表有两个操作数&#xff0c;如逻辑与运算符就是双目运算符&#xff1b;单目运算符代表有一个操作数&#xff0c; 如逻辑非运算符就是单目运算符。运算符也称操作符…

开发语言漫谈-SQL

SQL是另一个门类的开发语言&#xff0c;是专用于结构化数据库操作的专用语言。SQL不可能单独开发系统&#xff0c;但是做数据库方面的系统不懂SQL也不行。市面上很大部分开发岗位&#xff08;后台&#xff09;都是要和数据库打交道的&#xff0c;所以SQL必须掌握。 好消息是&am…

转:Learn Rust the Dangerous Way-系列文章翻译-总述

原文地址 太精彩了&#xff0c;不转不足以表达我的喜爱。 前言 《Learn Rust the Dangerous Way》​cliffle.com/p/dangerust/ 最近发现了一个学习Rust的优秀系列文章&#xff0c;本人准备对该系列文章进行翻译。 本文是《Learn Rust the Dangerous Way》系列文章翻译的第…

Centos 7部署sysbench测试Opengauss/MogDB性能

适配环境 操作系统版本&#xff1a;Red Hat Enterprise Linux Server release 7.9 (Maipo) 数据库版本&#xff1a;MogDB 5.0.3 mogdb快速安装 1、获取PTK $ curl --proto https --tlsv1.2 -sSf https://cdn-mogdb.enmotech.com/ptk/install.sh | sh 2、生成config.yaml $ ptk …

阿里巴巴fastjson实现复制

以下为真实案例&#xff0c;供日常开发使用 package com.somnus.json;import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.somnus.custom.domain.Area; import com.somnus.custom.domain.Employee; import com.somnus.custom.domain.Empl…

C 练习实例25

C 练习实例25 题目&#xff1a; 求12!3!...20!的和。 程序分析&#xff1a; 此程序只是把累加变成了累乘。 实例 #include <stdio.h>int main() {int i;long double sum,mix;sum0,mix1;for(i1;i<20;i){mixmix*i;sumsummix;} printf("%Lf\n",sum); }以…

Python exe 文件反编译为 Python 脚本

文章目录 前言版本反编译Python 可执行文件&#xff08;.exe&#xff09;反编译打包一个简单的 .exe 可执行文件提取 pyc 文件使用脚本提取使用工具提取 将 .pyc 文件转换为 Python 脚本入口运行类非入口运行类转换补全后的 pyc 文件uncompyle6 反编译在线工具 可能遇到的问题P…