windows phone 10的硬件后退按钮无法运行
本文关键字:按钮 运行 phone 硬件 windows | 更新日期: 2023-09-27 18:08:29
我有一个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"时,硬件返回按钮起作用。但是当默认页面是背景音乐页面时,硬件后退按钮不起作用-背景音乐页面是一个页面的背景音乐(也有一个播放和停止按钮)的应用程序
你可以试试这个吗:
下SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
把
HardwareButtons.BackPressed += OnBackRequested:
然后在OnBackRequested
HardwareButtons.BackPressed += new EventHandler<BackPressedEventArgs>((s, a) =>
{
解释是您需要预先注册事件处理程序。在您当前的代码中,您只处理软件返回按钮。当单击它并知道它是Phone平台时,您就可以注册一个新的事件处理程序。所以,我建议你做的是注册处理程序相同的软件后退按钮在前面,所以它处理相同的行为,你不需要添加新的事件处理程序,如果它是一个电话。
首先,在windows phone上,你是这样处理硬件返回键的:
if (App.DetectPlatform() == Platform.WindowsPhone)
{
HardwareButtons.BackPressed += new EventHandler<BackPressedEventArgs>((s, a) =>
{
if (rootFrame.CanGoBack)
{
rootFrame.GoBack();
a.Handled = true;
}
});
}
这是不对的,就像按两次后退键一样,当手机按下后退键时,rootFrame
的BackStack
中的两个项目就会被移除。你可以这样修改代码:
if (App.DetectPlatform() == Platform.WindowsPhone)
{
if (rootFrame.CanGoBack)
{
rootFrame.GoBack();
e.Handled = true;
}
}
其次,在你的BackgroundMusic
的OnNavigatedTo
事件中,我不知道你的mainFrame
是什么,如果这是BackgroundMusic
页面中的Frame
,那么它可以导航到MainPage
, rootFrame
的BackStack
将没有项目。如果这个mainFrame
恰好是rootFrame
,当它在OnNavigatedTo
事件上时,导航将失败,rootFrame
的项目计数BackStack
仍然保持为0。
所以如果你想用rootFrame
导航到MainPage
,而rootFrame
的默认页面是BackgroundMusic
,你可以在Loaded
事件中导航到MainPage
,例如:
private void BackgroundMusic_Loaded(object sender, RoutedEventArgs e)
{
var status = this.Frame.Navigate(typeof(MainPage));
}