检测从系统屏幕返回

时间:2014-11-07 09:18:28

标签: c# .net windows-phone-8 windows-phone-8.1

在某些时候,我的应用程序需要显示在可用网络列表中,这没问题:

private void ShowConnectionSettings()
{
    Debug.WriteLine("ShowConnectionSettings()");

    //Use the ConnectionSettingsTask to bring up the connection settings
    var connectionSettings = new ConnectionSettingsTask();

    // We are using the Connection Settings page for AirplaneMode.
    connectionSettings.ConnectionSettingsType = ConnectionSettingsType.WiFi;
    connectionSettings.Show();
}

如何检测用户何时再次移除该屏幕?我需要OnReturnFromSystemScreen事件或类似事件。

我做了一些测试:

private void PhoneApplicationPage_GotFocus(object sender, RoutedEventArgs e)
{
   txtHeader.Text = DateTime.Now.ToLongTimeString();
}

private void PhoneApplicationPage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    txtHeader.Text = DateTime.Now.ToLongTimeString();
}

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        txtHeader.Text = DateTime.Now.ToLongTimeString();

    }

但这还不够。所以任何想法都会有所帮助。

3 个答案:

答案 0 :(得分:1)

我认为没有从系统屏幕返回的事件,您可以使用最近的OnNavigatedTo并检查NavigationEventArgs.NavigationMode的值Back,这将指示是否您使用返回导航导航回页面。然后还要检查IsNavigationInitiator,它指示导航是否在应用中启动。像这样:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (e.NavigationMode == NavigationMode.Back && e.IsNavigationInitiator == false)
    {
        txtHeader.Text = DateTime.Now.ToLongTimeString();
    }
}

另请参阅http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationmode(v=vs.95).aspx了解NavigationMode的所有值。

答案 1 :(得分:1)

我错过了最明显的Activated和Deactivated事件

// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
   //do stuff
}

// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
   //do stuff
}

当我的系统屏幕显示时,调用Deactivated,当我返回Activated时调用。

答案 2 :(得分:0)

我的测试结果并不清楚。如果您在使用自己的代码获取事件时遇到问题:

"通常情况下,事件由类处理程序标记处理,并且不会引发GotFocus事件以供该控件上的任何用户代码处理程序处理。"

您需要显式覆盖OnGotFocus事件处理程序。

http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.gotfocus.aspx http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.control.ongotfocus.aspx