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

创建Frame单例,实现WPF页面跳转

需求:

有一个F0View主页面入口,三个子页面(First.xaml/Second.xaml/Third.xaml)用Frame默认加载第一个页面 First.xaml。实现三个页面之间顺序跳转,并且每个页面只初始化一次。

实现:

1,将三个页面放入统一容器中

 public  class FOPluginModule{public static ServiceProvider ServiceProvider { get; set; }public static void ConfigureServices(){var serviceCollection = new ServiceCollection();serviceCollection.AddSingleton<F0Instruction>();serviceCollection.AddSingleton<F0Procedure>();serviceCollection.AddSingleton<F0Result>();ServiceProvider = serviceCollection.BuildServiceProvider();}}

2,创建单例封装主视图中的Frame控件

using System.Windows.Controls;namespace WDM.MR.WMC.Plugins.F0.Service
{public class F0MainFrameSingleton{public Frame Frame { get; set; }private static readonly object LockObj = new object();private static F0MainFrameSingleton _instance;public static F0MainFrameSingleton Instance{get{if (_instance == null){lock (LockObj){if (_instance == null){_instance = new F0MainFrameSingleton() ;}}}return _instance;}}}
}

3,将主页面Frame实例化,在F0ViewModel中

 public F0ViewModel(Frame mainFrame)
{FOPluginModule.ConfigureServices();F0MainFrameSingleton.Instance.Frame = mainFrame;
}

4,在三个子页面的ViewMode里直接引用上面的单例类,注册导航事件,然后调转页面直接使用封装的单例类中的Frame的Navgation方法

下面是以其中一个页面为例,其他子页面同样的方法,ExtraData的值可以任意定义。

 1)跳转方法

[RelayCommand]
private void OnNextPage()
{F0MainFrameSingleton.Instance.Frame.Navigate(FOPluginModule.ServiceProvider.GetService(typeof(F0Procedure)), "Ins");
}

2)导航事件具体的逻辑处理

public F0InstructionViewModel()
{F0MainFrameSingleton.Instance.Frame.Navigated += Ins_Navigated;F0MainFrameSingleton.Instance.Frame.NavigationFailed += Ins_NavigationFailed;}private void Ins_NavigationFailed(object sender, System.Windows.Navigation.NavigationFailedEventArgs e)
{throw new NotImplementedException();
}private void Ins_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{if(e.ExtraData != null){Console.WriteLine("chenggong");}
}

以上可完成完整的跳转

用到哪,学到哪,记到哪

http://www.lryc.cn/news/365820.html

相关文章:

  • 正宇软件助力江西数字人大建设,高效解决群众“急难愁盼”问题
  • 打造AIPC轻量化方案 360AI浏览器及360AI搜索全新发布
  • 《effective c++》学习笔记
  • 11.盛水最多的容器
  • 通过在idea上搭建虚拟hadoop环境使用MapReduce做词频去重
  • AI技术变革与企业服务创新
  • 探秘Facebook:社交媒体的未来之路
  • rust的类型转换和一些智能指针用法(四)
  • 探索大模型技术及其前沿应用——TextIn文档解析技术
  • Java HashMap 扩容机制深度解析
  • 一、Electron 环境初步搭建
  • ffmpeg编码器编码元数据的过程以及编码前后的差异
  • AB测试学习(附有相关代码)
  • 用idea将java文件打成jar包
  • Ansible——group模块
  • Sql注入-报错注入
  • pyqt 回车触发两次editingFinished的解决办法
  • 爬取股票数据python
  • 每日新闻掌握【2024年6月4日 星期二】
  • 智谱AI 发布最新开源模型GLM-4-9B,通用能力超Llama-3-8B,多模态版本比肩GPT-4V
  • 从写简历到谈薪资的最全教程
  • Vue3 响应式API:高级函数(二)
  • 『大模型笔记』什么是提示词注入(Prompt Injection)攻击?
  • SD-WAN与IPSec的对比
  • Ceph入门到精通-ceph经典盘符飘逸问题处理步骤
  • 【CV算法工程师必看】作为一个图像算法工程师,需要会什么,要学哪些技术栈?
  • 【造化弄人:计算机系大学生真的象当年的高速公路收费员一样吗?】
  • 民主测评要做些什么?
  • JimuReport 积木报表 v1.7.5 版本发布,免费的低代码报表
  • Ubuntu安装Protobuf