【问题标题】:Hardware back button on windows phone 10 is not functioningwindows phone 10 上的硬件后退按钮不起作用
【发布时间】:2016-09-23 04:59:34
【问题描述】:

Windows phone 10 上的硬件后退按钮出现问题,当默认页面为 BackgroundMusic 页面时该按钮不起作用。

App.cs

protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif
            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;

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

            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(BackgroundMusic), e.Arguments);
                }
                // Ensure the current window is active
                Window.Current.Activate();
            }
        }

private void RootFrame_Navigated(object sender, NavigationEventArgs e)
        {
            // Each time a navigation event occurs, update the Back button's visibility  
            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 (App.DetectPlatform() == Platform.WindowsPhone)
            {
                HardwareButtons.BackPressed += new EventHandler<BackPressedEventArgs>((s, a) =>
                {
                    if (rootFrame.CanGoBack)
                    {

                        rootFrame.GoBack();
                        a.Handled = true;
                    }
                });
            }
            else if (App.DetectPlatform() == Platform.Windows)
            {
                if (rootFrame.CanGoBack)
                {
                    e.Handled = true;
                    rootFrame.GoBack();
                }
            }
        }

        public static Platform DetectPlatform()
        {
            bool isHardwareButtonsAPIPresent = ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons");

            if (isHardwareButtonsAPIPresent)
            {
                return Platform.WindowsPhone;
            }
            else
            {
                return Platform.Windows;
            }
        }

public static MediaElement GlobalMediaElement
        {
            get { return Current.Resources["MyPlayer"] as MediaElement; }
        }

        private void mediaended(object sender, RoutedEventArgs e)
        {
            var AppMediaElement = App.GlobalMediaElement;
            AppMediaElement.Position = TimeSpan.Zero;
            AppMediaElement.Play();
        }

BackgroundMusic.cs

const string soundTrackToken = "soundtrack";
    int flag = 1;


    public BackgroundMusic()
    {
        this.InitializeComponent();
    }

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        //navigationHelper.OnNavigatedTo(e);
        mainFrame.Navigate(typeof(MainPage));

        if (StorageApplicationPermissions.FutureAccessList.ContainsItem(soundTrackToken))
        {
            StorageFile audioFile = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(soundTrackToken);
            if (audioFile != null)
            {
                await StartAudio(audioFile);
            }
        }
    }

如何处理?

注意: - 当默认页面为MainPage时,硬件返回按钮起作用。但是当默认页面是BackgroundMusic页面时,硬件后退按钮不起作用 - BackgroundMusic页面是应用程序上的背景音乐页面(还有一个播放和停止按钮)。

【问题讨论】:

    标签: c# uwp back-button


    【解决方案1】:

    你能试试这个吗:

    SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
    

    HardwareButtons.BackPressed += OnBackRequested:
    

    然后在OnBackRequested里面删除这个

    HardwareButtons.BackPressed += new EventHandler<BackPressedEventArgs>((s, a) =>
                {
    

    解释是您需要预先注册事件处理程序。在您当前的代码中,您只处理软件后退按钮。当它被点击并知道它是 Phone 平台时,你就注册了一个新的事件处理程序。所以,我建议你在前面注册一个和软件后退按钮一样的处理程序,所以它处理相同的行为,如果以后是电话,你不需要添加新的事件处理程序。

    【讨论】:

      【解决方案2】:

      首先,在 Windows Phone 上,您可以像这样处理硬件后退按钮:

      if (App.DetectPlatform() == Platform.WindowsPhone)
      {
          HardwareButtons.BackPressed += new EventHandler<BackPressedEventArgs>((s, a) =>
          {
              if (rootFrame.CanGoBack)
              {
                  rootFrame.GoBack();
                  a.Handled = true;
              }
          });
      }
      

      这不对,就像处理两次后退键一样,在手机上按下后退键时,rootFrameBackStack 中的两个项目将被删除。您可以像这样更改此代码:

      if (App.DetectPlatform() == Platform.WindowsPhone)
      {
          if (rootFrame.CanGoBack)
          {
              rootFrame.GoBack();
              e.Handled = true;
          }
      }
      

      其次,在你的BackgroundMusicOnNavigatedTo事件中,我不知道你的mainFrame是什么,如果这是BackgroundMusic页面里面的Frame,那么可以导航到@ 987654331@,rootFrameBackStack 将没有项目。如果这个mainFrame 正好是rootFrame,则在OnNavigatedTo 事件上导航将失败,BackStack 的项目计数rootFrame 仍然为0。

      因此,如果您想在rootFrame 的默认页面为BackgroundMusic 时使用rootFrame 导航到MainPage,您可以在Loaded 事件中导航到MainPage,例如这样:

      private void BackgroundMusic_Loaded(object sender, RoutedEventArgs e)
      {
          var status = this.Frame.Navigate(typeof(MainPage));
      }
      

      【讨论】:

      • 我想在 MainPage 上显示 BackgroundMusic 页面(因此,在 MainPage 中有一个 MediaElement 和播放/暂停按钮)。它旨在在MainPage和所有页面上添加背景音乐(可以在所有页面上播放的背景音乐)。
      • @Rose,如果要播放背景音频,则不需要MediaElement,即使在前台使用媒体播放器播放,您可以参考我在此case 中的回答。如果你想在MainPage上显示BackgroundMusic页面,那么你可以在OnLaunched方法中导航到MainPage并在MainPage中放置一个Frame控件,使用这个Frame导航到BackgroundMusic页面.
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多