简体   繁体   English

从App.xaml.cs导航

[英]Navigating from App.xaml.cs

I want to add an application bar to multiple pages of my app. 我想在我的应用程序的多个页面中添加一个应用程序栏。 So, I'm defining the application bar as an application resource so that it can be used by multiple pages. 因此,我将应用程序栏定义为应用程序资源,以便它可以被多个页面使用。 Now, the event handlers for these buttons are in the App class as mentioned here http://msdn.microsoft.com/en-us/library/hh394043%28v=VS.92%29.aspx . 现在,这些按钮的事件处理程序位于App类中,如此处所述http://msdn.microsoft.com/en-us/library/hh394043%28v=VS.92%29.aspx But, these app bar buttons are basically shortcuts to important pages. 但是,这些应用栏按钮基本上是重要页面的快捷方式。 So, clicking a button would just take you to the corresponding page. 因此,单击按钮会将您带到相应的页面。 But, since I'm defining the event handlers in App.xaml.cs , it doesn't allow me to navigate. 但是,因为我在App.xaml.cs定义事件处理程序,所以它不允许我导航。 I understand the reason for this. 我理解这个的原因。 But, I don't know how to solve the problem. 但是,我不知道如何解决这个问题。

NavigationService.Navigate(new Uri("/Counting.xaml", UriKind.RelativeOrAbsolute));

says "An object reference is required for the non-static field, method or property System.Windows.Navigation.NavigationService.Navigate(System.Uri)" 说“非静态字段,方法或属性System.Windows.Navigation.NavigationService.Navigate(System.Uri)”需要对象引用“

Does it work if you get access to the frame? 如果您可以访问框架,它是否有效?

(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Counting.xaml", UriKind.RelativeOrAbsolute));

Edit: Each application has only one Frame . 编辑:每个应用程序只有一个框架 It's this frame that exposes the NavigationService . 这个框架暴露了NavigationService Therefore, the NavigationService is always accessible via the frame since there's always an instance of it in any Windows Phone app. 因此,始终可以通过框架访问NavigationService,因为在任何Windows Phone应用程序中始终都有它的实例。 Since you don't usually instantiate a new NavigationService , it's easy to think that it's a static method. 由于您通常不会实例化新的NavigationService ,因此很容易认为它是静态方法。 However, it's actually a non-static class that gets instantiated automatically when your app is run. 但是,它实际上是一个非静态类,可以在运行应用程序时自动实例化。 All you're doing in this case is getting the global instance, which is attached to the always-present Frame, and using that to navigate between pages. 在这种情况下,您所做的就是获取全局实例,该实例附加到始终存在的Frame,并使用它在页面之间导航。 This means your class does not have to instantiate, or explicitly inherit, a NavigationService. 这意味着您的类不必实例化或显式继承NavigationService。

an other way to navigate to an other page from App.xaml.cs (using the app bar) is using the rootFrame var (at the end line): 另一种从App.xaml.cs导航到其他页面的方法(使用应用栏)使用rootFrame var(在结束行):

private Frame rootFrame = null;
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
    ...
    SettingsPane.GetForCurrentView().CommandsRequested += App_CommandRequested;
}

private void App_CommandRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
{
SettingsCommand cmdSnir = new SettingsCommand("cmd_snir", "Snir's Page", 
              new Windows.UI.Popups.UICommandInvokedHandler(onSettingsCommand_Clicked));
args.Request.ApplicationCommands.Add(cmdSnir);
}

void onSettingsCommand_Clicked(Windows.UI.Popups.IUICommand command)
{
if (command.Id.ToString() == "cmd_snir")
      rootFrame.Navigate(typeof(MainPage)); //, UriKind.RelativeOrAbsolute);

}

I found this approach a better one. 我发现这种方法更好。 The RootFrame object is already in the App.xaml.cs file, you just need to call it. RootFrame对象已经在App.xaml.cs文件中,您只需要调用它。 Also putting this in a UI thread dispatcher is safer. 将它放在UI线程调度程序中也更安全。

 Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    // change UI here
                    RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM