繁体   English   中英

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

[英]Splitview with frame and navigating to another page, back button does not work

我已经制作了简单的汉堡菜单,并且工作正常,但是使用“ 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();
        }
    }
}

谢谢

我已经制作了简单的汉堡菜单,并且工作正常,但是使用“ myFrame”后退按钮导航到另一页后不起作用(应用关闭)。 如果我使用this.Frame导航,效果很好,后退按钮效果很好,但第二页上看不到splitview。

根据上面提供的代码,您为rootFrame的Navigated事件注册了处理程序RootFrame_Navigated,以设置Back按钮的可见性,并且注册的BackRequested事件也用于处理rootFrame的GoBack 因此,仅当您使用rootFrame导航到另一个页面时,“后退”按钮才会显示并起作用 ,而当您使用myFrame时,它将不起作用

如何制作我的代码,以便在第二页上进行splitview且后退按钮仍然有效?

根据您的描述,您应该为myFrame的Navigated事件注册一个处理程序以设置“后退”按钮的可见性,并注册BackRequested事件以在MainPage.xmal.cs中处理myFrame的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));
    }
}

这是整个样本供您参考,下面是输出:

在此处输入图片说明

暂无
暂无

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

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