Navigating from App.xaml.cs

前端 未结 3 1256
执笔经年
执笔经年 2021-02-20 12:46

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

相关标签:
3条回答
  • 2021-02-20 12:59

    I found this approach a better one. The RootFrame object is already in the App.xaml.cs file, you just need to call it. Also putting this in a UI thread dispatcher is safer.

     Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        // change UI here
                        RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                    });
    
    0 讨论(0)
  • 2021-02-20 13:12

    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):

    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);
    
    }
    
    0 讨论(0)
  • 2021-02-20 13:13

    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. Therefore, the NavigationService is always accessible via the frame since there's always an instance of it in any Windows Phone app. Since you don't usually instantiate a new NavigationService, it's easy to think that it's a static method. 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. This means your class does not have to instantiate, or explicitly inherit, a NavigationService.

    0 讨论(0)
提交回复
热议问题