Splitview带框架并导航到另一个页面,后退按钮不起作用

时间:2016-05-20 20:23:12

标签: c# uwp

我已经制作了简单的汉堡包菜单并且工作正常但在使用" myFrame"导航到另一个页面后后退按钮不起作用(应用程序关闭)。

如果我使用this.Frame导航它工作正常,后退按钮工作正常但在第二页上不能看到splitview。

如何制作我的代码,以便我在第二页上有splitview,后退按钮stil可以工作?

public MainPage()
{
    this.InitializeComponent();
}

private void btnHamburger_Click(object sender, RoutedEventArgs e)
{
    myFrame.Navigate(typeof(SecondPage));
}
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <RelativePanel>
        <Button Name="btnHamburger" FontFamily="Segoe MDL2 Assets" Content="&#xE700;" 
            FontSize="24" Click="btnHamburger_Click" Margin="0,0,-4,0" Width="48"/>
    </RelativePanel>
    <SplitView Name="mySV" Grid.Row="1" DisplayMode="CompactInline" OpenPaneLength="200" 
        CompactPaneLength="48" HorizontalAlignment="Left"  Margin="0,0,0,0">
        <SplitView.Pane>
            <TextBlock></TextBlock>
        </SplitView.Pane>
        <SplitView.Content>
            <Frame Name="myFrame"></Frame>
        </SplitView.Content>
    </SplitView>
</Grid>

上面的代码我在第二页看到了汉堡菜单,但后退按钮不起作用。

此代码适用于后退按钮,但我无法在第二页看到汉堡包:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void btnHamburger_Click(object sender, RoutedEventArgs e)
    {
        this.Frame.Navigate(typeof(SecondPage));
    }
}

后退按钮代码:

    public App()
    {
        Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
            Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
            Microsoft.ApplicationInsights.WindowsCollectors.Session);
        this.InitializeComponent();
        this.Suspending += OnSuspending;
    }

    /// <summary>
    /// Invoked when the application is launched normally by the end user.  Other entry points
    /// will be used such as when the application is launched to open a specific file.
    /// </summary>
    /// <param name="e">Details about the launch request and process.</param>
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {

        Frame rootFrame = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();

            rootFrame.NavigationFailed += OnNavigationFailed;
            rootFrame.Navigated += RootFrame_Navigated;
            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }

        if (e.PrelaunchActivated == false)
        {
            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }

        // Register a handler for BackRequested events and set the  
        // visibility of the Back button  
        SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;

        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
            rootFrame.CanGoBack ?
            AppViewBackButtonVisibility.Visible :
            AppViewBackButtonVisibility.Collapsed;

    }

    /// <summary>
    /// Invoked when Navigation to a certain page fails
    /// </summary>
    /// <param name="sender">The Frame which failed navigation</param>
    /// <param name="e">Details about the navigation failure</param>
    void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
    {
        throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
    }

    /// <summary>
    /// Invoked when application execution is being suspended.  Application state is saved
    /// without knowing whether the application will be terminated or resumed with the contents
    /// of memory still intact.
    /// </summary>
    /// <param name="sender">The source of the suspend request.</param>
    /// <param name="e">Details about the suspend request.</param>
    private void OnSuspending(object sender, SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        //TODO: Save application state and stop any background activity
        deferral.Complete();
    }

    private void RootFrame_Navigated(object sender, NavigationEventArgs e)
    {

        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
            ((Frame)sender).CanGoBack ?
            AppViewBackButtonVisibility.Visible :
            AppViewBackButtonVisibility.Collapsed;
    }

    private void OnBackRequested(object sender, BackRequestedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;

        if (rootFrame.CanGoBack)
        {
            e.Handled = true;
            rootFrame.GoBack();
        }
    }
}

由于

1 个答案:

答案 0 :(得分:2)

  

我已经制作了简单的汉堡包菜单并且工作正常但在使用&#34; myFrame&#34;导航到另一个页面后后退按钮不起作用(应用程序关闭)。   如果我使用this.Frame导航它工作正常,后退按钮工作正常但在第二页上不能看到splitview。

根据您在上面提供的代码,您为 rootFrame的导航事件注册处理程序RootFrame_Navigated以设置Back按钮的可见性,并且注册的BackRequested事件也用于处理 rootFrame的GoBack 。因此,返回按钮仅在您使用rootFrame 导航到其他页面时才会显示和工作,并且在您使用myFrame时无法正常工作

  

如何制作我的代码,以便我在第二页上有splitview,后退按钮仍可用?

根据您的说明,您应该为 myFrame的导航事件注册处理程序以设置“后退”按钮的可见性,并注册BackRequested事件以处理 myFrame&#39 ; MainPage.xmal.cs 中的GoBack

<强> MainPage.xaml.cs中:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        // set an initial page for myFrame
        myFrame.Navigate(typeof(Page1));
        // register a handler for myFrame's Navigated event to set the visibility of the Back button
        myFrame.Navigated += myFrame_Navigated;
        // register BackRequested event to handle myFrame's GoBack
        SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
    }

    private void myFrame_Navigated(object sender, NavigationEventArgs e)
    {
        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
            ((Frame)sender).CanGoBack ?
            AppViewBackButtonVisibility.Visible :
            AppViewBackButtonVisibility.Collapsed;
    }

    private void OnBackRequested(object sender, BackRequestedEventArgs e)
    {
        if (myFrame.CanGoBack)
        {
            e.Handled = true;
            myFrame.GoBack();
        }
    }

    private void btnHamburger_Click(object sender, RoutedEventArgs e)
    {
        myFrame.Navigate(typeof(SecondPage));
    }
}

以下是Entire Sample供您参考,以下是输出:

enter image description here