加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[UWP] [C#]覆盖Appx.cs中的背景导航集

发布时间:2020-12-15 23:27:22 所属栏目:百科 来源:网络整理
导读:有点卡在这里.我有一个splitview应用程序,当按下反键键驻留在Appx.cs中时,它具有向后导航的事件. 我想在splitviews内容页面中导航的其中一个页面中定义一个不同的操作(例如,当某个元素可见时可以关闭该元素)但是应用程序始终遵循appx.cs中设置的事件,并忽略
有点卡在这里.我有一个splitview应用程序,当按下反键键驻留在Appx.cs中时,它具有向后导航的事件.

我想在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中的OnBackRequested方法正在处理我的应用程序中的后退导航.
>我在splitview内容窗格中打开了一个页面(让我们称之为PageTwo.xaml).
>在PageTwo.xaml中,我希望在按下OnBackRequested时有一个事件
>我现在不能,因为Appx.cs中的那个总是被称为
>我想知道是否有办法在PageTwo.xaml.cs页面中触发OnBackRequested,而不是Appx.cs页面中的OnBackRequested.

任何帮助,将不胜感激.我试图让这个工作失去理智:P

解决方法

事件处理程序按照添加的顺序触发,因此这方面没有运气.另一种方法是制作自己的活动,包装原始活动.在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;

确保在离开页面时取消订阅该事件,否则可能会导致内存泄漏.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读