C#学习教程:从App.xaml.cs导航因此,我将应用栏定义为应用资源,以便多个页面可以使用它。这些按钮的事件处理程序现在位于App类中,如此处所述http://msdn.microsoft.com/en-us/library/hh394043%28v=VS.92%29.aspx。然而,这些应用栏按钮基本上是重要页面的快捷方式。所以点击一个按钮会带你到相应的页面。但是,因为我在App.xaml.cs中定义了事件处理程序,所以它不允许我进行导航。我明白这是为什么。但是,我不知道如何解决这个问题。NavigationService.Navigate(newUri("/Counting.xaml",UriKind.RelativeOrAbsolute));说“非静态字段、方法或属性System.Windows.Navigation.NavigationService.Navigate(System.Uri)”需要对象引用“如果您可以访问框架,它是否有效?(Application.Current.RootVisualasPhoneApplicationFrame).Navigate(newUri("/Counting.xaml",UriKind.RelativeOrAbsolute));编辑:每个应用程序只有一个框架。这个框架暴露了NavigationService。所以,NavigationService总是可以通过框架访问,因为总是有在任何WindowsPhone应用程序中的一个实例。由于您通常不会实例化一个新的NavigationService,因此很容易认为它是一个静态方法。然而,它实际上是一个非静态类,可以在应用程序运行时自动实例化。在在这种情况下,您所做的就是获取全局实例,它附加到始终存在的框架,并使用它在页面之间导航。这意味着您的类不必实例化或显式继承NavigationService。从App.xaml.cs导航到其他页面(使用应用栏)的另一种方法是使用rootFramevar(在结束行中):privateFramerootFrame=null;protectedoverrideasyncvoidOnLaunched(LaunchActivatedEventArgsargs){...SettingsPane。GetForCurrentView().CommandsRequested+=App_CommandRequested;}privatevoidApp_CommandRequested(SettingsPanesender,SettingsPaneCommandsRequestedEventArgsargs){SettingsCommandcmdSnir=newSettingsCommand("cmd_snir","Snir'sPage",newWindows.UI.Popups.UICommandInvokedHandler(onSettingsCommand_Clicked));args.Request.ApplicationCommands.Add(cmdSnir);}voidonSettingsCommand_Clicked(Windows.UI.Popups.IUICommandcommand){if(command.Id.ToString()=="cmd_snir")rootFrame.Navigate(typeof(MainPage));//,UriKind.RelativeOrAbsolute);我发现这种方法更好。RootFrame对象已经在App.xaml.cs文件中,您只需调用它即可。放在UI线程调度器中也更安全。以上就是C#学习教程:分享自App.xaml.cs导航的全部内容,如果对你有用,还需要了解更多C#学习教程,希望大家多多关注——Deployment.Current。Dispatcher.BeginInvoke(()=>{//在此处更改UIRootFrame.Navigate(newUri("/MainPage.xaml",UriKind.Relative));});本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
