Archive for February, 2011

WP7 and Isolated Storage

Image: renjith krishnan / FreeDigitalPhotos.net
This post contains some good information for using isolated storage on a Windows Phone device.

First of all there are basically three ways an application would like to store data.

  • Temporary data and files
    Your application might need to create temporary data and files while running, then Isolated storage would be a good option. Although just remember to clear the data when it is no longer needed or being used. Create a specific folder that you can regularly clear.
  • User generated data
    Your application might provide the user with the possibility to store notes, photos etc. One thing to take into account here is to provide the user with the possibility to delete this data as well.
  • Application Data
    Settings, selected items in a list etc.

Updating/uninstalling an Application
When an application is updated with a new version, its isolated storage folder will not be touched or modified. So it is up to you to make sure that after an update that the data is still valid and not causing any exceptions/errors. You might need to migrate or transform data to work with the new version.

IsolatedStorageSettings
The easiest way of storing data in isolated storage is to use IsolatedStorageSettings class.

IsolatedStorageSettings.ApplicationSettings["SettingKey"] = "my setting value";

This will get an instance of IsolatedStorageSettings that contains the contents of the application’s IsolatedStorageFile, scoped at the application level, or creates a new instance of IsolatedStorageSettings if one does not exist. When the user exits your application, Windows Phone will automatically call Save to store your application settings.

IsolatedStorageFile
In my application I want to store XML statistics data that is dynamically created during runtime. The Statistics object is a custom class of a Tree data structure. When the application starts I call LoadData and on exit I call SaveData.

To save data to IsolatedStorageFile do as following

        public void SaveData()
        {
            XmlWriterSettings writerSettings =
                new XmlWriterSettings
                {
                    Indent = true,
                    IndentChars = "\t"
                };

            // Save Statistics
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = 
                        store.OpenFile("Statistics.dat", FileMode.Create))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(TreeNode<int>));
                        using (XmlWriter xmlWriter = XmlWriter.Create(stream, writerSettings))
                        {
                            serializer.Serialize(xmlWriter, Statistics);
                        }

                        stream.Close();
                    }
                }
            }
            catch { }
        }

The statistics content will be saved in a file called ”Statistics.dat” in the isolated storage store for the application.

To load data from isolated storage

        public void LoadData()
        {

            XmlSerializer serializer = new XmlSerializer(typeof(TreeNode<int>));
            // Try to load statistics from Isolated Storage
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = store.OpenFile("Statistics.dat", FileMode.Open))
                    {

                        Statistics = (TreeNode<int>)serializer.Deserialize(stream);
                    }
                }
            }
            catch { }
        }

Here you should catch IsolatedStorageException to handle if the file does not exist if you need custom handling for that.
Note, when working with files make sure that you properly use the “Using” keyword to ensure correct use of IDisposable objects.

Advertisements

WP7 and using the Tilt Effect

Windows Phone and Metro Design gives us a new intuitive and userfriendly UI, to increase this UX even more we can use a Tilt Effect on controls. By using a Tilt Effect the users get much clearer feedback that he or she has actually successfully pressed for example a button. The tilt effect provides you with additional visual effects/feedback for control interactions by using motions during state changes. You get a tilting like response when the control is touched.

To get access to the Tilt Effect, you can either download the Control Tilt Effect Sample, and add the TiltEffect.cs file to you project or simply add a reference to WP7 toolkit. In most WP7 projects I have built I have used more than the tilt effect from the toolkit so I usually end up adding a reference to the toolkit.

Namespace declaration
Add a namespace declaration to access the tilt effect. Either locally or to the toolkit

xmlns:local="clr-namespace:[NAMESPACE]" 

or

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

Enable Tilt Effect
You can then enable tilt effect for a single control or several at the same time by setting TiltEffect.IsTiltEnabled=”True”.

  • Single control
    <Button Content="Click me" toolkit:TiltEffect.IsTiltEnabled="True" />
    
  • Multiple controls
                        <StackPanel toolkit:TiltEffect.IsTiltEnabled="True">
                            <Button Content="settings" Click="OnSettings"  />
                            <Button Content="feedback" Click="OnFeedback"  />
                            <Button Content="about" Click="OnAbout" />
                        </StackPanel>
    

You can even put it at page level to have all controls in your page tilt enabled.

Suppress Effect
If you have enabled tilt effect for multiple controls, then you might need to Suppress the effect for one or more controls. Fortunately this is also very easy, all you have to do is to set TiltEffect.SuppressTilt=”True” on all controls that should not be tilt enabled.

toolkit:TiltEffect.SuppressTilt="True"

Example where last button is not tilt enabled

                    <StackPanel toolkit:TiltEffect.IsTiltEnabled="True">
                        <Button Content="settings" Click="OnSettings"  />
                        <Button Content="feedback" Click="OnFeedback"  />
                        <Button Content="about" Click="OnAbout" toolkit:TiltEffect.SuppressTilt="True" />
                    </StackPanel>

Determine Light or Dark Theme in WP7

When you create WP7 applications it is important that you test your application using both Light and Dark theme, and also try it with different accent colors. If you are unlucky your application could be rejected if you have not tested it with the light theme.

In my recent WP7 application I wanted to change the panorama background image depending on light or dark theme. So how do I do that? Well it is actually pretty simple to find out what theme is currently used. By using the static resources PhoneLightThemeVisibility I can figure out if the Light theme is currently beeing used.

            Visibility v = (Visibility)Resources["PhoneLightThemeVisibility"];
            if (v == System.Windows.Visibility.Visible)
            { . . . }

A pretty nice way of finding out what theme is beeing used.

%d bloggers like this: