![](/img/trans.png)
[英]Lost DataContext when navigating back in Frame (SplitView) in UWP
[英]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=""
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.