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ページは、アプリケーションにバックグラウンドミュージックのページ(再生と停止ボタンもあります)です。
メインページのページ(MediaElementと再生/一時停止ボタンがあります)が表示されます。これは、メインページとすべてのページ(すべてのページで再生できるバックグラウンドミュージック)にバックグラウンドミュージックを追加することを目指しています。 – Rose
@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'ページに移動できます。 –