[UWP][C#]覆盖Appx.cs中的反压导航设置

[UWP][C#]覆盖Appx.cs中的反压导航设置,c#,windows,uwp,splitview,C#,Windows,Uwp,Splitview,这里有点卡住了。我有一个splitview应用程序,当按下位于Appx.cs中的backkey时,该应用程序具有向后导航事件 我想在splitviews内容页内导航的其中一个页面中定义不同的操作(例如,当某个元素可见以关闭该元素时),但是应用程序始终遵循appx.cs中设置的事件,并忽略在内容框中加载的页面中的事件。以下是appx.cs中的代码: protected async override void OnLaunched(LaunchActivatedEventArgs e) {

这里有点卡住了。我有一个splitview应用程序,当按下位于Appx.cs中的backkey时,该应用程序具有向后导航事件

我想在splitviews内容页内导航的其中一个页面中定义不同的操作(例如,当某个元素可见以关闭该元素时),但是应用程序始终遵循appx.cs中设置的事件,并忽略在内容框中加载的页面中的事件。以下是appx.cs中的代码:

protected async override void OnLaunched(LaunchActivatedEventArgs e)
{
    // Do not repeat app initialization when the Window already has content,
    // just ensure that the window is active
    if (Window.Current.Content == null)
    {
        // Create a Frame to act as the navigation context and navigate to the first page
        _rootFrame = new Frame();
        _rootFrame.NavigationFailed += OnNavigationFailed;
        _rootFrame.Navigated += OnNavigated;

        if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
        {
            //TODO: Load state from previously suspended application
        }

        // Place the frame in the current Window
        Window.Current.Content = new MainPage(_rootFrame);

        // 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;
    }
}

private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
    if (_rootFrame != null && _rootFrame.CanGoBack)
    {
        e.Handled = true;
        _rootFrame.GoBack();

    }
}
下面是加载到splitviews内容窗格的其中一个页面中的代码:

public CalcOutputPage()
{
    this.InitializeComponent();


    // add page specific handling of back navigation when entering this page
    SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
}

// page specific back button navigation
private void OnBackRequested(object sender, BackRequestedEventArgs e)
{

    if (MobileStackPanel.Visibility == Visibility.Visible)
    {
       MobileStackPanel.Visibility = Visibility.Collapsed;
        e.Handled = true;
    }
    else
    {
        e.Handled = false;
    }
}
但这不起作用。如果它是加载到导航堆栈中的第一个页面,则它可以工作,但在所有其他时间,应用程序都遵循appx.cs中的导航说明

因此,为了澄清:

  • Appx.cs中的onbackrequest方法正在处理我的应用程序中的返回导航
  • 我在splitview内容窗格中打开了一个页面(我们称之为PageTwo.xaml)
  • 在PageTwo.xaml中,我希望在按下OnBackRequest时有一个事件
  • 我现在不能,因为Appx.cs中的那个总是被调用的
  • 我想知道是否有办法在PageTwo.xaml.cs页面中请求onback,而不是在Appx.cs页面中请求onback

任何帮助都将不胜感激。试图让它工作起来,我真是疯了:事件处理程序的触发顺序与它们添加的顺序相同,所以这方面运气不好。另一种选择是制作自己的活动,将原始活动包装起来。在
App.xaml.cs
中:

public event EventHandler<BackRequestedEventArgs> BackRequested;

private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
    // Raise child event
    var eventHandler = this.BackRequested;

    if (eventHandler != null)
    {
        eventHandler(sender, e);
    }

    if (!e.Handled)
    {
        if (_rootFrame != null && _rootFrame.CanGoBack)
        {
            e.Handled = true;
            _rootFrame.GoBack();

        }
    }
}

请确保在离开页面时取消订阅该活动,否则可能会导致内存泄漏。

真糟糕。这正是我想要的!谢谢实际上我没有想到要尝试这个。您现在为我的代码库添加了一些内容。:)
((App)(App.Current)).BackRequested += OnBackRequested;