Friday, 30 December 2011

Tombstoning and Mango: Something’s Awry!

I’ve wasted another day.

I’ve been trying without success to understand what’s happening with Tombstoning, and why my properties which are data bound are not being updated.

To cut a long story short, you can reproduce this using Matt Lacey’s Tombstone Helper Toolkit as follows:

First, download the latest source from CodePlex.

Build for OS 7.1, which requires you to modify the Demo Projects WMAPPManifest.xml file to have the correct AppPlatformVersion 7.1, or you get an error from Microsoft.Phone.PreImport.targets ValidateWMAppManifest rule.

Go to the Debug tab of the demo, and switch on Tombstoning.

Build and run the file, and go to the TextBox demo, and verify that if you change field 1, and navigate away and back, that its value returns.

Now make the following changes:

On line 30 of TextBoxes.xaml, change the line to this, so it’s DataBound as follows:

<TextBox Name="first" Text="{Binding Path=UserName, Mode=TwoWay}"/>

Now, add this to the top of TextBoxes.xaml.cs, inside the namespace declaration.  It’s based on Jesse Liberty's ViewModel here:

public class MainPageViewModel : INotifyPropertyChanged {
private string _userName;
public string UserName
get { return _userName; }
set { _userName = value; NotifyPropertyChanged("UserName"); }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propName)
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propName)); }

public MainPageViewModel()
UserName = "I AM SAM";

Finally, modify Matt’s TextBoxes constructor to set my ViewModel as the DataContext:

public TextBoxes()
DataContext = new MainPageViewModel();

If you now run the code, and change “I AM SAM” to “SAM I AM”, navigate away, and then back, you’ll see that the name is not replaced.


I’ve found a bodge, based on what I read here on the WP7 forums:  Change OnNavigatedTo like this:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
// Uncomment the next line for changes to be reloaded
// Loaded += (s, ea) =>


If you uncomment the Loaded+= line, the code will now work as expected.

Clearly there’s something odd going on with Mango here, as I’ve seen this in my own simplistic implementation where I set the Text property of a TextBox directly without all the framework like this:

Long.Text = (string)State["Longitude"];

and it too does not get updated correctly.

No comments:

Post a Comment