12 January 2011

Tombstoning MVVMLight ViewModels with SilverlightSerializer on Windows Phone 7

I’ve been down this road before but I thought it wise to revisit this subject, since I see a lot of people in the Windows Phone 7 developer community still struggling with the concept of tombstoming. In my previous attempt to make some universal way of tombstoning ViewModels on Windows Phone 7 I used DataContractSerializer, which is a nice idea but does not work very well with MVVMLight since the ViewModelBase is not serializable. And sometimes you have to go trough a lot of hooplah if stuff you use in your code does not turn out to be serializable after all.There is a better way, I think. So here it is: universal tombstoning for MVVMLight, take two.

Enter SilverlightSerializer by the as far as I am concerned immortal Mike Talbot. Download this thing and store it somewhere in your sources. To use it for tombstoning, I have created the following extension methods:

using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using GalaSoft.MvvmLight;

namespace LocalJoost.Utilities
{
  public static class ApplicationExtensions
  {
    private static string GetIsFile( Type t)
    {
      return string.Concat(t.Name, ".dat");
    }

    public static T RetrieveFromIsolatedStorage<T>(this Application app) 
      where T : class
    {
      using (var userAppStore = 
         IsolatedStorageFile.GetUserStoreForApplication())
      {
        var dataFileName = GetIsFile(typeof(T));
        if (userAppStore.FileExists(dataFileName))
        {
          using (var iss = userAppStore.OpenFile(dataFileName, FileMode.Open))
          {
             return SilverlightSerializer.Deserialize(iss) as T;
          }
        }
      }
      return null;
    }

    public static void SaveToIsolatedStorage(this Application app, 
                                      ViewModelBase model)
    {
      var dataFileName = GetIsFile((model.GetType()));
      using (var userAppStore = 
               IsolatedStorageFile.GetUserStoreForApplication())
      {
        if (userAppStore.FileExists(dataFileName))
        {
          userAppStore.DeleteFile(dataFileName);
        }
        using (var iss = userAppStore.CreateFile(dataFileName))
        {
          SilverlightSerializer.Serialize(model,iss);          
        }
      }
    }
  }
}

Then I go to the App.xaml.cs and modify it as depicted next:

private void Application_Launching(object sender, LaunchingEventArgs e)
{
  LoadModel();
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{
  LoadModel();
}

private void LoadModel()
{
  try
  {
    MyMainViewModel.Instance = this.RetrieveFromIsolatedStorage<MyMainViewModel>();
  }
  catch (Exception){ }
  if( MyMainViewModel.Instance == null) MyMainViewModel.CreateNew(); 
}

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
  this.SaveToIsolatedStorage(MyMainViewModel.Instance);
}

private void Application_Closing(object sender, ClosingEventArgs e)
{
  this.SaveToIsolatedStorage(MyMainViewModel.Instance);
}

In red you see my additions to the standard generated App.xaml.cs. You will also need to add at least one “using” statement (using LocalJoost.Utilities) and a second one with the namespace in which you have put MyMainViewModel. Notice the fact that there is an empty try-catch around the RetrieveFromIsolatedStorage call. This is intentional. Since binary deserialization tends to throw exceptions if you have changed the ViewModel’s code, you always want to make sure your application gets served a working ViewModel where ever it needs to come from.

The skeleton of a MainViewModel looks something like this in my household:

public class MyMainViewModel : ViewModelBase
{
  public MyMainViewModel( )
  {
  }  

  private static MyMainViewModel _instance;
  public static MyMainViewModel Instance
  {
    get {return _instance;}
    set { _instance = value; }
  }

  public static void CreateNew()
  {
    if (_instance == null)
    {
      _instance = new MyMainViewModel();
    }
  }
}

If your want certain properties of your ViewModel specifically not to be serialized (because they come from a – in this example omitted – application model) you can mark them with the [DoNotSerialize] attribute that comes with SilverlightSerializer.

You can now always bind to MyMainViewModel.Instance and you will either get a fresh new or a retrieved from isolated storage ViewModel. The fun thing is that although MVVMLight’s ViewModelBase is not serializable, the SilverlightSerializer does in fact serialize it which makes life a whole lot easier.

You can, by the way, also decide to store the ViewModel on the PhoneApplicationService on deactivation and retrieve it there on activation. IMHO always storing on isolated storage gives a more consistent feeling to an application. But that’s just me.

A few important points to conclude this post:

  • A ViewModel that is to be serialized on isolated storage should always have a default constructor.
  • Properties of the ViewModel that you want to have serialized need to have a getter and a setter (believe me, you can spend some time overlooking this).
  • Do not try something clever like this on the static instance property of your ViewModel:
public static MyMainViewModel Instance
{
  get {return _instance ?? (_instance = new MyMainViewModel());}
  set { _instance = value; }
}
If you do this, MyMainViewModel.Instance = this.RetrieveFromIsolatedStorage will first call the getter, thus creating a new instance of MyMainViewModel whatever happens next, then overwrite it with the retrieved version. But if you do something like registering a message in the constructor of MyMainViewModel, the initially created model will stay alive ‘somewhere’ - you will end up with two instances of your supposedly singleton instance running in you App, both listening to the same message and reacting to it - and spend, like me, a not so nice and probably extended period of  time scratching your head and wondering what the hell is going on.

I hope that with this post I once and for all made it easier for the Windows Phone 7 community to tackle something apparently tricky as tombstoning thus improving the quality of what we are all going to put in the App Hub.

28 December 2010

Enum values can be characters too

Almost everyone who has programmed something in C# has created an enum at one point or another:

public enum Whatever
{
  Zero,
  One,
  Two,
  Three
}
You can cast an enum value to an int - you then get a value assigned by the compiler based upon the order in the enum, so Zero will be 0, One will be 1, etc. If you have a particular sick mind you can add a new chapter to How to write unmaintainable code by creating something like
public enum Whatever
{
  One = 5,
  Two = 3,
  Three = 2
}
But you can also assign values to it using characters. This way, I have created an enumeration that can be used to retrieve tiles from Google servers based upon their enum value without the switch I wrote in my infamous "Google Maps for Windows Phone 7" article. So now you can define the server on which Google stores a particular tile type like this:
public enum GoogleType
{
  Street         ='m',
  Hybrid         ='y',
  Satellite      ='s',
  Physical       ='t',
  PhysicalHybrid ='p',
  StreetOverlay  ='h',
  WaterOverlay   ='r'
}
and then determine the server like this:
var server = string.Format("http://mt{0}.google.com", (char)GoogleType.Street);

This makes far cleaner code.

09 December 2010

WiX- A Developer's Guide to Windows Installer XML

And now for something completely different – a review of someone else’s examples. I have been reading “WiX- A Developer's Guide to Windows Installer XML“ by Nick Ramirez. I would not dare call myself a WiX expert – in fact I wrote one three-component setup in the cause of a few weeks, but I was asked to review this book by its publisher, Packt Publisher and well, here’s my verdict.

Examples examples examples

What I like about the book: it takes you by the hand using what I think is the most powerful tool in teaching: examples. Which is what I so painfully missed when I myself was struggling with WiX. Lots of concepts and half-*ssed postings on forums, but no complete examples. In the first chapter the author starts a small example which gets more and more elaborate. After reading that first chapter you have a good idea of the most important concepts of installing. The second chapter follows the same recipe and gives you more of the fine details on creating files and directories. If you have read the first two chapters, you can write a basic installer.

Pick and choose

The rest of the book is more of the pick-and-choose type. It still follows the same recipe, but whereas the first two chapters are mandatory, the rest is optional – use what you need, although I would recommend not skipping chapter 5, “Understanding the Installation Sequence”

What do I miss?

I was puzzled by the absence of  a description of the util:XmlFile task, which can be utilized for modifying config files during install. Apart from that, the sub titles says “Create a hassle-free installer for your Windows software using WiX”. That’s exactly what it delivers. I would very much have liked something on how you create and install web sites, handle the intricacies of creating applications pools, set the ASP.NET version, permissions, create CGI support settings and stuff like that – taking into account people can install on different operating systems and different versions of IIS. Maybe something for a second print? ;-)

Conclusion

First part learns you the basics pretty well, the rest is a solid reference. A book I should have bought and read before I started mucking around with WiX.

28 November 2010

Converter for showing/hiding Silverlight/WP7 GUI elements with boolean model values

Typically, when you are writing Silverlight or Windows Phone 7 applications using the MVVM pattern, your viewmodels expose boolean values indicating something is going on – for instance, your background thread is loading data and thus you want to display a progress bar. For some reason the designers of XAML choose the Visibility property of a GUI element to be of type “Visibility”, an enum with values “Visible” and “Collapsed”.

Anyway, you could of course make your viewmodel expose properties of type Visibility but as an architect I would call that “bad form”. It’s so much easier to create a simple converter for this:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace LocalJoost.Converters
{
  public class VisibilityConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, 
                   CultureInfo culture)
    {
      if (value != null && value is bool && parameter != null)
      {
        var bValue = (bool) value;
        var visibility = (Visibility)Enum.Parse(
             typeof (Visibility), parameter.ToString(),true);
        if (bValue) return visibility;
        return visibility == 
           Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
      }
      
      return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                       CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }
}

This convertor assumes a boolean input value and a parameter of type Visibility. If the input value is true, it returns the value in the parameter, if it is false, it returns the opposite. So it’s basically a converter that converts one type of boolean in another, with the possibility to specify what is true and what is false ;-)

Typical usage snippet:

Visibility="{Binding IsDownloading, Converter={StaticResource VisibilityConverter}, ConverterParameter=Visible}"

which says that if the “IsDownloading” property of the model is true, the GUI object in case must be visible. Since I actually used this snippet on a Windows Phone 7 ProgressBar, this makes perfect sense.

13 November 2010

WIX configurable search & replace custom action for text files

Recently I had my first encounter with actually writing setups myself using WIX. This is a powerful albeit a bit complex technology that allows you to build MSI setups using XML files. It includes all kinds of tasks to modify files after installation, and thus you can for instance change the settings in configuration files based upon user input. Unfortunately good examples are few and far between, and I hope to save some poor sod a lot of time with this article

Trouble is that the standard tasks only know how to modify XML files. Now this is usually enough, but if you want to change plain text files files then you are basically on your own. WIX supports the idea of custom actions that you can write in C#, So I set out to write such a search & replace custom action that was to be configured by a custom table.

That turned out less straightforward than I thought. Under Windows 7 and Vista, with UAC enabled, part of the installation sequence is run with elevated rights, part is not, and your custom actions are running not with elevated rights unless they are ‘deferred’ but then you don’t have access to the custom table anymore. I spent quite some time figuring out why my configuration files were virtualized by the installer but those in the program directory themselves never got changed. Finally solved that catch-22 by following what appeared to be a beaten track: split the task into two task. The first task runs immediate, reads the table, and dumps its contents in a file, the second runs deferred.

The actual code consist out of two methods and a property. The class declaration with the property looks like this:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Microsoft.Deployment.WindowsInstaller;

namespace LocalJoost.Wix
{
  public class CustomActions
  {
    /// <summary>
    /// Gets the search and replace data file location. 
 /// This is stored in the user's temp directory
    /// and used by the installer.
    /// </summary>
    /// <value>The search and replace data file.</value>
    private static string SearchAndReplaceDataFile
    {
      get
      {
       return Environment.GetEnvironmentVariable("TEMP") + 
            Path.DirectorySeparatorChar + 
           "SearchAndReplace.xml";
      }
    }
  }
}

This defines a hard coded XML file in the installer user's temp directory. Then, the first method that actually gathers the information and writes it into said XML file:

/// <summary>
/// This method should be declared with Execute="immediate" 
/// and called with Before="InstallFinalize"
/// Use in conjunction with SearchAndReplaceExec
/// </summary>
/// <param name="session">The session.</param>
/// <returns></returns>
[CustomAction]
public static ActionResult SearchAndReplaceInit(Session session)
{
  session.Log("Begin SearchAndReplaceInit");
  File.Delete(SearchAndReplaceDataFile);
  if (session.Database.Tables.Contains("SearchAndReplace"))
  {
     var lstSearchAndReplace = new List<SearchAndReplaceData>();
     using (var propertyView = 
      session.Database.OpenView("SELECT * FROM `SearchAndReplace`"))
     {
       propertyView.Execute();
       foreach (var record in propertyView)
       {
         var token = new SearchAndReplaceData
         {
           File = session.Format(record["File"].ToString()),
           Search = session.Format(record["Search"].ToString()),
           Replace = session.Format(record["Replace"].ToString())
         };
         lstSearchAndReplace.Add(token);
       }
     }
     var serializer = new TypedXmlSerializer<List<SearchAndReplaceData>>();
     serializer.Serialize(SearchAndReplaceDataFile, lstSearchAndReplace);
  }
  else
  {
    session.Log("No SearchAndReplace custom table found");
  }
  session.Log("End SearchAndReplaceInit");
  return ActionResult.Success;
}
and finally the method that reads the XML file and actually executes the search and replace actions
/// <summary>
/// This method should be decleared with Execute="deferred" 
/// and called with Before="InstallFinalize"
/// Use in conjunction with SearchAndReplaceInit
/// </summary>
/// <param name="session">The session.</param>
/// <returns></returns>
[CustomAction]
public static ActionResult SearchAndReplaceExec(Session session)
{
  session.Log("Begin SearchAndReplaceExec");
  if (File.Exists(SearchAndReplaceDataFile))
  {
    var serializer = new TypedXmlSerializer<List<SearchAndReplaceData>>();
    var tokens = serializer.Deserialize(SearchAndReplaceDataFile);
    tokens.ForEach(token =>
    {
      try
      {
        string fileContents;
   
        var file = new FileInfo(token.File);
        {
          if (file.Exists)
          {
            using (var reader = new StreamReader(file.OpenRead()))
            {
              fileContents = reader.ReadToEnd();
              reader.Close();
            }
            fileContents = fileContents.Replace(token.Search, token.Replace);
         
            using (var writer = new StreamWriter(file.OpenWrite()))
            {
              writer.Write(fileContents);
              writer.Flush();
              writer.Close();
            }
          }
        }
      }
      catch (Exception)
      {
        session.Log("Could not process file " + token.File);
      }
    });
    File.Delete(SearchAndReplaceDataFile);
  }
  session.Log("End SearchAndReplaceExec");
  return ActionResult.Success;
}
Attentive readers will have noticed this code actually uses two companion classes: SearchAndReplaceData:
namespace LocalJoost.Wix
{
  public class SearchAndReplaceData
  {
    public string File { get; set; }
    public string Search { get; set; }
    public string Replace { get; set; }
  }
}
and TypedXmlSerializer:
using System.Collections;
using System.IO;
using System.Xml.Serialization;

namespace LocalJoost.Wix
{
  public class TypedXmlSerializer<T> 
  {
    public void Serialize(string path, T toSerialize)
    {
      var serializer = new XmlSerializer(typeof(T));
      using (var fileStream = 
      new FileStream(path, FileMode.Create))
      {
        serializer.Serialize(fileStream, toSerialize);
        fileStream.Close();
      }
    }

    public T Deserialize(string path)
    {
      var serializer = new XmlSerializer(typeof(T));
      T persistedObject;
      using (var reader = new StreamReader(path))
      {
        persistedObject = (T)serializer.Deserialize(reader);
        reader.Close();
      }
      return persistedObject;
    }
  }
}
If you got this all up and running, actually using it means taking three steps. First you have to declare them in right into the top Product tag like this:
<CustomAction Id="SearchAndReplaceInit"
    BinaryKey="LJWix"
    DllEntry="SearchAndReplaceInit"
    Execute="immediate"/>

<CustomAction Id="SearchAndReplaceExec"
    BinaryKey="LJWix"
    DllEntry="SearchAndReplaceExec"
    Execute="deferred" Impersonate="no"/>
 
<Binary Id="LJWix" SourceFile="LocalJoost.Wix.CA.dll" />
This assumes that your WIX custom actions projects was called "LocalJoost.Wix" and your resulting dll is called “LocalJoost.Wix.CA.dll”. Here you see the use of "immediate" for the information gathering task and the "deferred" for the actual executing task. The second step is embedding the custom actions into the install execution sequence:
<InstallExecuteSequence>
  <Custom Action="SearchAndReplaceInit" Before="InstallFinalize"/>

  <Custom Action="SearchAndReplaceExec" Before="InstallFinalize"/>
</InstallExecuteSequence>
If you have looked closely at the SearchAndReplaceInit task, you see it's trying to read a custom SearchAndReplace table, so the third and final step is to define and actually fill that table:
<CustomTable Id="SearchAndReplace">
  <Column Id="Id" Type="string" Category="Identifier" PrimaryKey="yes"/>
  <Column Id="File" Type="string"/>
  <Column Id="Search" Type="string"/>
  <Column Id="Replace" Type="string"/>
  <Row>
    <Data Column="Id">id1</Data>
    <Data Column="File" >[INSTALLLOCATION]Somedirectory\Somefile.txt</Data>
    <Data Column="Search">Text to search for</Data>
    <Data Column="Replace">Text to replace this by</Data>
  </Row>
  <Row>
    <Data Column="Id">id2</Data>
    <Data Column="File" >[INSTALLLOCATION]Somedirectory\Someotherfile.txt</Data>
    <Data Column="Search">Some other text to search for</Data>
    <Data Column="Replace">Some other text to replace this by</Data>
  </Row>
</CustomTable>

Custom tables are also declared in the Product tag. The Id tag in the table is there because there seems to be an Id necessary, and the rest in pretty obvious: in the File tag you put the file you want to process, in Search what you want to search for, and in Replace what you want to replace it by. Duh :-) . And that’s pretty much what it there to it.

Now I would love to take credit for this, but the ideas behind it – not to mention significant parts of the code – were supplied by Kevin Darty who kindly assisted me by using his Twitter account when I was struggling with this. In the end I changed a lot of this code, but the samples he sent me saved me a lot of time. And true to the spirit of this blog and my promise to Kevin, I give back to the .NET community what it gave to me.

12 November 2010

Silverlight and Windows Phone 7 do not like DTDs

My current Windows Phone 7 project requires the possibility of reading configurations from various servers. For the GIS lovers among my audience: I am trying to read a GetCapabilities document from a WMS server. I was reading this one and this one, and the first one succeeded, while the second one failed. It became even more odd when I tried to write unit tests running from the full framework – then both links processed flawlessly. The error was “NotSupportedException” on XDocument.Load().

The second links contains a DTD from ye goode olde days and it turns out Silverlight and Windows Phone 7 by default are not very fond of DTD’s in documents. After some searching around I found the DtdProcessing Enumeration and I changed this code

var doc = XDocument.Load(stream)

into

using (var reader = XmlReader.Create(stream, 
  new XmlReaderSettings {DtdProcessing = DtdProcessing.Ignore}))
{
  var doc = XDocument.Load(reader)
}

And indeed, problem solved

02 November 2010

Swapping two elements in a bound ObservableCollection IMPROVED VERSION

Today I noticed something odd. I wanted to swap two elements in an ObservableCollection that was bound to a list box – so I could move objects up and down in the list box. I noticed that moving up always worked, and moving down led to a crash in my application with the message ‘The parameter is incorrect’ in the Application_UnhandledException method of App.Xaml. Fat lot of help that is, I can tell ya ;-)

Maybe this is a Windows Phone 7 only thing, but after a bit of experimenting I found out that you always have to change the value of the item that is last in the collection – i.e., the item with the highest index – first. This worked perfectly and I blogged about it. Later I found one exception, when I tried to swap the first and the second entry. Swapping second and first works, but the other way around gave once again ‘The parameter is incorrect’

This makes no sense at all, but I guess it has something to do with the fact that at one point in time the collection contains one object twice, and whatever is bound to it does not seem to like that very much. One of my household sayings is that were’s a will, there’s a workaround. So, second time around:

using System.Collections.Generic;
using System.Collections.ObjectModel;

using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace LocalJoost.Utilities
{
  public static class ObservableCollectionExtensions
  {
    public static void Swap<T>(
       this ObservableCollection<T> collection, 
       T obj1, T obj2)
    {  
      if (!(collection.Contains(obj1) && collection.Contains(obj2))) return;
      var indexes = new List<int>
         {collection.IndexOf(obj1), collection.IndexOf(obj2)};
      if(indexes[0] == indexes[1]) return;
      indexes.Sort();
      var values = new List<T> {collection[indexes[0]], collection[indexes[1]]};
      collection.RemoveAt(indexes[1]);
      collection.RemoveAt(indexes[0]);
      collection.Insert(indexes[0], values[1]);
      collection.Insert(indexes[1], values[0]);
    }
  }
}
and now I am really done with it. It's not always high science what I blog, just things I hope that save someone else a lot of time :-)