In the previous episode of this series about how to develop a Windows Phone application from scratch we found out that special care must be taken when data must be passed between an application and its PeriodicTask. You learned how data can be protected against mutual access by using a Mutex object. You also learned that you cannot pass data directly from one to another, because the application and its PeriodicTask execute inside different processes.
In this episode of EvenTiles you will learn how you can make use of IsolatedStorage to pass data from the application to the PeriodicTask. Since we already created a separate project in the EvenTiles solution that is taking care of passing data, we can simply modify functionality in that project (EvenTilesComm) to use a file to pass data between the application and its PeriodicTask. Data protection against mutual access is already in place, so we can concentrate on file access. Hopefully the design decision in part 13 to make use of private methods that are called each time we access a public property inside the TileData class starts to make sense now.
What we want to achieve is the following:
- From inside the application we can store a string containing content for the backside of a Secondary Tile in a file at any time
- Our PeriodicTask executes approximately once per 30 minutes and it either displays the string passed by the application on the backside of the Secondary Tile or it displays a default string that is defined inside the PeriodicTask
- The content of the backside of the Secondary Tile needs to toggle each time the PeriodicTask executes
- If the EvenTiles user modifies the string to be displayed inside the Settings page, it will immediately be displayed on the Secondary Tile (if it exists)
In order to pass data between the application and the PeriodicTask we will extend the private retrieve / store methods inside the TileData class by adding a call to a couple of other private methods. Those new private methods will use a file in IsolatedStorage to read / write data from, meaning we can effectively pass data between the application and its PeriodicTask.
- private static string RetrieveSecondaryBackContent()
- {
- mtx.WaitOne();
- try
- {
- RetrieveTileContent();
- return secondaryBackContent;
- }
- finally
- {
- mtx.ReleaseMutex();
- }
- }
- private static void StoreSecondaryBackContent(string content)
- {
- mtx.WaitOne();
- try
- {
- secondaryBackContent = content;
- PersistTileContent();
- }
- finally
- {
- mtx.ReleaseMutex();
- }
- }
A new string to be displayed on the back side of a Secondary Tile will be stored by the application each time users modify their own tile text in the Settings Page of the EvenTiles application (something that will also be done initially when the application starts). The PeriodicTask simply retrieves that data (since now the data is persisted in a file this will work properly) and displays it on the back side of the Secondary Tile. Each time data needs to be stored a new file is created in IsolatedStorage or an existing file is overwritten. Each time data needs to be retrieved, we check if a file containing that data exists. There are small ways to optimize data access in the TileData class, because right now we simply read / write all file content when retrieving / storing single property values. However, this approach simplifies the code and the overhead with only two different variables is very small.
- private const string contentFileName = "EvenTileContent.txt";
- private static void PersistTileContent()
- {
- using (var store = IsolatedStorageFile.GetUserStoreForApplication())
- {
- using (var contentStream = new StreamWriter(store.CreateFile(contentFileName)))
- {
- contentStream.WriteLine(showDefaultSecondaryBackContent.ToString());
- contentStream.WriteLine(secondaryBackContent);
- }
- }
- }
- private static void RetrieveTileContent()
- {
- using (var store = IsolatedStorageFile.GetUserStoreForApplication())
- {
- if (store.FileExists(contentFileName))
- {
- using (var contentStream = new StreamReader(store.OpenFile(contentFileName, FileMode.Open)))
- {
- showDefaultSecondaryBackContent = Convert.ToBoolean(contentStream.ReadLine());
- secondaryBackContent = contentStream.ReadToEnd();
- }
- }
- }
- }
With the way we organized the functionality inside the TileData class in the previous episode of this development series, these are the only new methods necessary in order to pass data. When the user installs a Secondary Tile for the EvenTiles application and modifies its back string content through the Settings page, this is the result (changing the back content of the Secondary Tile every 30 minutes):
The following video shows EvenTiles in action with proper transfer of data between the application and its PeriodicTask.
To be able to experiment with this working implementation of EvenTiles, especially to understand how the application interacts with the PeriodicTask through a file in IsolatedStorage, the sample code is available for dowload here.
Right now we have the basic functionality of EvenTiles more or less ready, although the About Page still needs to get some content. That is something we will work on in the next part of EvenTiles. After that we will cover much more in the upcoming episodes of EvenTiles including but not limited to
- using ads in the application
- retrieving location information inside the application
- taking pictures
- modifying pictures
- using alarms and notifications
- using Visual Studio’s integrated Performance Analysis to find performance bottlenecks inside the application
- submitting the application for certification
EvenTiles will continue soon so stay tuned for the next episode.
If you want to see EvenTiles already in action on your Windows Phone, you can also install the latest version from Marketplace. Remember that this application is not meant to be extremely useful, although it contains similar functionality that “serious” applications have. Just go ahead and get your free copy of EvenTiles from Marketplace at this location: http://www.windowsphone.com/en-US/search?q=EvenTiles (or search on your phone for EvenTiles in the Marketplace application).