需求:
有一个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");}
}
以上可完成完整的跳转
用到哪,学到哪,记到哪