【问题标题】:Windows Phone 7: Difficulty with app opening/closing events on emulatorWindows Phone 7:模拟器上的应用程序打开/关闭事件有困难
【发布时间】:2010-08-30 02:03:48
【问题描述】:

我在 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()

奇怪的是,即使模拟器没有显示应用程序的打开或关闭,这些也会被命中。

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

我在这里做错了什么?

【问题讨论】:

    标签: c# windows-phone-7


    【解决方案1】:

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

    我发现,如果您设置了在启动时遇到的断点,并且您没有继续一定的时间(例如

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 谢谢,但此代码仍然无法正常工作。 (我保存了一个包含 9 个元素的集合,然后取出一个包含 0 的集合。)你知道为什么会这样吗,还是我应该提出一个新问题?
      • 我们可以在问题中看到您的处理程序(desactived/actived)的一些代码吗?
      • 模拟器关闭时似乎重置了IsolatedStorage系统:codebadger.com/blog/post/2010/09/03/…(文档结尾)。您是否尝试将您的状态保存到 PhoneApplicationService.Current.State 中?
      • 知道如何使用 VisualStudio 2012 做到这一点吗?点击开始后调试会话不会停止...
      猜你喜欢
      • 2011-11-06
      • 1970-01-01
      • 2014-03-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 2011-02-22
      相关资源
      最近更新 更多