2016-09-23 17 views
0

Windows Phone 10のハードウェアの戻るボタンに問題があります。これは、デフォルトページがページBackgroundMusicの場合には機能しません。Windows Phone 10のハードウェア戻るボタンが機能しない

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ページは、アプリケーションにバックグラウンドミュージックのページ(再生と停止ボタンもあります)です。

答えて

0

あなたはこの試みることができます:

SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; 

の下

は説明はあなたがする必要があるということですOnBackRequested

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

の内側にこれを削除し、その後

HardwareButtons.BackPressed += OnBackRequested: 

を置きますイベントハンドラを前もって登録します。現在のコードでは、ソフトウェアの戻るボタンのみを処理します。それがクリックされ、Phoneプラットフォームであることがわかったら、新しいイベントハンドラを登録します。だから、あなたがやるべきことは、ハンドラをソフトウェアバックボタンと同じように登録することです。同じ挙動を扱うので、後で電話の場合は新しいイベントハンドラを追加する必要はありません。 Windowsの携帯電話に、あなたがこのようなハードウェアの戻るボタンを取り扱うすべての

0

まず、:

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

これは適切ではない、rootFrameBackStackに二回、2つの項目のバックボタンを処理するのに似ていますモバイルで戻るボタンを押すと削除されます。あなたはこのようにこのコードを変更することができます。

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

第二に、あなたのBackgroundMusicOnNavigatedToイベントで、私はこれがBackgroundMusicページ内のFrameであれば、それはナビゲートすることができ、あなたのmainFrame何であるかを知りませんMainPageに、BackStackrootFrameには項目がありません。このmainFrameがちょうどrootFrameの場合、OnNavigatedToイベントのときはナビゲーションに失敗し、BackStackの数値はrootFrameのままです。

あなたがrootFrameのデフォルトページがBackgroundMusicときMainPageにナビゲートするrootFrameを使用したいのであれば、あなたはLoadedイベントにすることができます。このように、たとえばMainPageに移動します。私はBackgroundMusicを表示したい

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

メインページのページ(MediaElementと再生/一時停止ボタンがあります)が表示されます。これは、メインページとすべてのページ(すべてのページで再生できるバックグラウンドミュージック)にバックグラウンドミュージックを追加することを目指しています。 – Rose

+0

@Rose、バックグラウンドオーディオを再生したい場合は、メディアプレーヤーを使用してフォアグラウンドでも、 'MediaElement'は必要ありません。このケースで私の答えを参照することができます(http://stackoverflow.com/questions/39318800/uwp-binding-system-media-transport-controls-xaml-media-transport-controls/39323503#39323503)。また、MainPageにBackgroundMusicページを表示したい場合は、 'OnLaunched'メソッドで' MainPage'に移動し、 'MainPage'の中に' Frame'コントロールを入れて、 'FrameMay'を使用して' BackgroundMusic'ページに移動できます。 –

関連する問題