我在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页面是应用程序上的背景音乐页面(还有一个播放和停止按钮)。
答案 0 :(得分:0)
你可以试试这个:
在
下SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
把
HardwareButtons.BackPressed += OnBackRequested:
然后在OnBackRequested
HardwareButtons.BackPressed += new EventHandler<BackPressedEventArgs>((s, a) =>
{
解释是您需要预先注册事件处理程序。在当前代码中,您只处理软件后退按钮。单击它并知道它是Phone平台,然后注册一个新的事件处理程序。所以,我建议你做的是预先注册处理程序与软件后退按钮相同,因此它处理相同的行为,如果以后是手机,你不需要添加新的事件处理程序。
答案 1 :(得分:0)
首先,在Windows手机上你处理硬件后退按钮是这样的:
if (App.DetectPlatform() == Platform.WindowsPhone)
{
HardwareButtons.BackPressed += new EventHandler<BackPressedEventArgs>((s, a) =>
{
if (rootFrame.CanGoBack)
{
rootFrame.GoBack();
a.Handled = true;
}
});
}
这是不对的,就像处理后退按钮两次一样,当在移动设备上按下后退按钮时,BackStack
的{{1}}中的两个项目将被删除。您可以像这样更改此代码:
rootFrame
其次,在if (App.DetectPlatform() == Platform.WindowsPhone)
{
if (rootFrame.CanGoBack)
{
rootFrame.GoBack();
e.Handled = true;
}
}
的{{1}}事件中,我不知道你的OnNavigatedTo
是什么,如果这是BackgroundMusic
mainFrame
1}}页面,然后可以导航到Frame
,BackgroundMusic
的{{1}}将没有项目。如果此MainPage
恰好是BackStack
,那么导航将在rootFrame
事件中导致失败,其中的项目为&#39}。 mainFrame
的{{1}}次仍为0。
因此,如果您希望在rootFrame
的默认网页为OnNavigatedTo
时使用BackStack
导航至rootFrame
,则可以在rootFrame
事件导航中导航以MainPage
为例,例如:
rootFrame