【发布时间】: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