Windows Phone 7:模拟器上的应用程序打开/关闭事件的难度

时间:2010-08-30 02:03:49

标签: c# windows-phone-7

我在wp7模拟器中遇到奇怪的行为。

我有一个死的简单应用程序,主要是直接来自VS 2010生成的模板。

来自 App.xaml

    <!--Required object that handles lifetime events for the application-->
    <shell:PhoneApplicationService 
        Launching="Application_Launching" Closing="Application_Closing" 
        Activated="Application_Activated" Deactivated="Application_Deactivated"/>

来自 App.xaml.cs 的逻辑删除代码:

    private void LoadSettings()
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        ICollection<TicTacSpot> getSpots;
        if (settings.TryGetValue<ICollection<TicTacSpot>>("spots", out getSpots))
        {
            Spots = getSpots;
        }

        if (Spots == null)
        {
            Spots = new List<TicTacSpot>();
        }
    }

    private void SaveSettings()
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        settings["spots"] = Spots;
    }

    // Code to execute when the application is launching (eg, from Start)
    // This code will not execute when the application is reactivated
    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        LoadSettings();
    }

    // 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)
    {
        LoadSettings();
    }

    // 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)
    {
        SaveSettings();
    }

    // Code to execute when the application is closing (eg, user hit Back)
    // This code will not execute when the application is deactivated
    private void Application_Closing(object sender, ClosingEventArgs e)
    {
        SaveSettings();
    }

看起来很简单。我在所有这些方法中设置了断点。当我点击F5部署应用程序时,被点击的事件处理程序是:

Application_Launching() Application_Deactivated()

奇怪的是,即使模拟器没有显示应用程序的打开或关闭,这些都会受到影响。

在模拟器中,我打开应用程序,玩游戏,关闭它,然后重新打开它。我使用“后退”和“开始”按钮来关闭它。尽管如此,我还是无法让任何事件处理程序再次被击中。

我在这里做错了什么?

2 个答案:

答案 0 :(得分:2)

调试会话是否仍处于活动状态?

我发现,如果您设置的断点在启动时遇到并且您没有继续一段时间(例如<10秒),则您的调试会话将被断开连接。操作系统将终止应用程序。

答案 1 :(得分:2)

像Dennis所说,要调试Activated事件处理程序,您需要在按下后退按钮后启动新的调试会话。顺序是:

  • 启动调试
  • 使用该应用,点击开始
  • 退出应用程序,调试会话已停止
  • 点击按钮 - 模拟器上的黑屏
  • 启动调试会话,快速:) 10秒后,操作系统终止应用程序