我在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页面是应用程序上的背景音乐页面(还有一个播放和停止按钮)。
发布于 2016-09-26 10:34:38
你能试试这个吗:
在……下面
SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;放
HardwareButtons.BackPressed += OnBackRequested:然后在OnBackRequested中删除它
HardwareButtons.BackPressed += new EventHandler<BackPressedEventArgs>((s, a) =>
{解释是,您需要预先注册事件处理程序。在您当前的代码中,您只处理软件的后退按钮。当它被单击并知道它是Phone平台时,您就注册了一个新的事件处理程序。所以,我建议你做的就是像前面的后退按钮一样注册处理程序,这样它就可以处理相同的行为,如果它以后是电话,你就不需要添加新的事件处理程序。
发布于 2016-09-26 10:42:47
首先,在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事件时,导航将失败,项的计数BackStack of rootFrame仍然保持为0。
因此,如果您想要在rootFrame的默认页面为BackgroundMusic时使用rootFrame导航到MainPage,您可以在Loaded事件中导航到MainPage,例如:
private void BackgroundMusic_Loaded(object sender, RoutedEventArgs e)
{
var status = this.Frame.Navigate(typeof(MainPage));
}https://stackoverflow.com/questions/39652908
复制相似问题