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

以上可完成完整的跳转

用到哪,学到哪,记到哪

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值