WPF Frame应用 实现页面跳转

需求:

有一个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");
    }
}

以上可完成完整的跳转

用到哪,学到哪,记到哪

相关推荐

  1. WPF Frame应用 实现页面

    2024-06-11 01:04:02       10 阅读
  2. uniapp 安卓应用设置等页面

    2024-06-11 01:04:02       24 阅读
  3. Vue3+ts实现页面及参数传递

    2024-06-11 01:04:02       168 阅读
  4. UniApp登录后如何实现页面

    2024-06-11 01:04:02       44 阅读
  5. php/js:实现几秒后进行页面

    2024-06-11 01:04:02       35 阅读
  6. [Vue3]-router实现基本的页面

    2024-06-11 01:04:02       30 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-11 01:04:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-11 01:04:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-11 01:04:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-11 01:04:02       20 阅读

热门阅读

  1. 中介子方程十一

    2024-06-11 01:04:02       8 阅读
  2. 对象创建的4种模式

    2024-06-11 01:04:02       10 阅读
  3. centos中sudo: apt: command not found

    2024-06-11 01:04:02       10 阅读
  4. k8s-CCE使用node节点使用VIP--hostNetwork&hostPort

    2024-06-11 01:04:02       8 阅读
  5. 2244. 完成所有任务需要的最少轮数

    2024-06-11 01:04:02       11 阅读
  6. debugger(四):源代码

    2024-06-11 01:04:02       7 阅读
  7. 第四章 Three.js 绘制基本几何体

    2024-06-11 01:04:02       9 阅读
  8. Spark RDD算子

    2024-06-11 01:04:02       11 阅读
  9. MySQL中的一行记录是怎么存储的

    2024-06-11 01:04:02       7 阅读