2016-05-20 23 views
0

簡単なハンバーガーメニューを作っても問題ありませんが、「myFrame」を使って別のページに移動した後、戻るボタンは機能しません。フレームを分割して別のページに移動すると戻るボタンが機能しない

このフレームを使用してナビゲートするとうまくいきますが、戻るボタンは正常に機能しますが、splitviewは2ページ目に表示されません。

私のコードをどのようにして2番目のページにsplitviewがあり、その戻るボタンが機能するのですか?私は2ページ目にハンバーガーメニューを見るが、バックボタンが動作しません上記のコードでは

<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 MainPage() 
{ 
    this.InitializeComponent(); 
} 

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

このコードは、バックボタンに動作しますが、私は2ページ目にハンバーガーを参照してくださいカント:

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(); 
     } 
    } 
} 

おかげ

答えて

2

私がしました単純なハンバーガーメニューを作ったが、うまくいきますが、 "myFrame"を使って別のページに移動した後、戻るボタンが機能しません(アプリケーションが閉じます)。 this.Frameを使用してナビゲートするとうまくいきますが、戻るボタンは正常に機能しますが、splitviewは2ページ目に表示されません。

あなたは上記のコードによると、あなたは、ハンドラは、[戻る]ボタンの可視性を設定するrootFrameのナビゲートイベントためRootFrame_Navigated登録、および登録BackRequestedイベントもrootFrameのGoBackのを処理するために使用されます。したがって、戻るボタンは、を使用して別のページに移動する場合にのみ表示され、動作します。は、myFrameを使用すると機能しません。

私のコードを作成する方法私はsplitviewを2番目のページに持っていますが、その戻るボタンはまだ動作しますか?あなたの説明によると、あなたはをMyFrameのGoBackのでMainPage.xmal.csを処理するために、[戻る]ボタンの可視性を設定し、BackRequestedイベントを登録するをMyFrameのナビゲートイベントのためのハンドラを登録する必要があります

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

+0

おかげで百万:) – Toni0123

関連する問題