Wednesday, September 29, 2010

State management in Windows Phone 7


There are 2 types of states you want to save on windows phone 7. One is when user leaves your application and another one is when your application is tombstoned (deactivated by launching some other application from shell, like phone is ringing, phone is locked and so on).

In a first case persistent data is saved into Isolated storage. Using of isolated storage is covered deeply in a number of places on net. So, in this case I put as a reference following link.

Small hint: all the data you save to isolated storage must be serializable.

Another hint - how to check is entity seriazable or not -
  
private static bool IsSerializable(T obj)
{
return ((obj is ISerializable) ||
(Attribute.IsDefined(typeof (T), typeof (SerializableAttribute))));
}


In a case another app forces your application closing deactivation event occurs and your handler on this event in app.xaml is executed. Keep in mind following - you have only 10 seconds to save your transient data. If you don't save them in 10 seconds data is discarded and exception raises.

Hint: In this case data is saved into IDictionary<string,object> property called State on PhoneApplicationPage class. Keep in mind following - "The state is only accessible during or after the OnNavigatedTo(NavigationEventArgs) method is called. Also during or before the OnNavigatedFrom(NavigationEventArgs) method is called. If you implement it too early or too late it will throw an exception. You should not use this property for excessive storage as there is a limit of 2MB for each page and 4MB for the entire application."

Cheers

Thursday, September 9, 2010

Accelerometer readings from WP7


Recently I've found interesting image which explains how axes values are represented over sensor on windows phone 7 device. Once when you register event handler for ReadingChanged event you'll get values for x,y and z axes over AccelerometerReadingEventArgs argument.

Argument contains property for x, y and z points. The values are expressed in G-forces (1G = 9.81 m/s2), so a value of 1.0 means that the corresponding axis is being pulled with the same force as the gravity in Paris. For example, if Z is -1.0, then the device is lying flat, face up, on a perfectly flat surface. If Z is 1.0, then the device is lying flat, faced down.

Keep in mind that acceleration reading change event occurs very often (50 times per second) and that you deal with a lot of data.