Windows Phone 7 Application Lifecycle: Listing 1

Saving transient application state.

private DateTime sessionStartedAt;
private double ActiveSessionDuration {
  get { return DateTime.Now.Subtract(sessionStartedAt).TotalMilliseconds; }
  set { sessionStartedAt = DateTime.Now.AddMilliseconds(-value); }
}

private void Application_Launching(object sender, LaunchingEventArgs e) {
  sessionStartedAt = DateTime.Now;
}

private void Application_Activated(object sender, ActivatedEventArgs e) {
  ActiveSessionDuration=
    (double)PhoneApplicationService.Current.State["SessionDuration"];
}

private void Application_Deactivated(object sender, DeactivatedEventArgs e) {
  PhoneApplicationService.Current.State["SessionDuration"] = 
    ActiveSessionDuration;
}

private void Application_Closing(object sender, ClosingEventArgs e){
  Debug.WriteLine("Session active for: " + DateTime.Now.Subtract(sessionStartedAt).TotalSeconds);
}
comments powered by Disqus

Featured

Subscribe on YouTube