Introducing Silverlight Book: Code Update

If you're reading Introducing Silverlight and are having some trouble with the code in Chapter 8 for the tracking textblocks sample, there's a missing line of code that defines a global variable.

Here's the full C# code for the TextBlocks sample:

    1: using System;
    2: using System.Windows;
    3: using System.Windows.Controls;
    4: using System.Windows.Documents;
    5: using System.Windows.Ink;
    6: using System.Windows.Input;
    7: using System.Windows.Media;
    8: using System.Windows.Media.Animation;
    9: using System.Windows.Shapes;
   10:  
   11: namespace SilverlightProject2
   12: {
   13:     public partial class Page : Canvas
   14:     {
   15:         string strCurrentText;
   16:         public void Page_Loaded(object o, EventArgs e)
   17:         {
   18:             // Required to initialize variables
   19:             InitializeComponent();
   20:             foreach (TextBlock tt in this.Children)
   21:             {
   22:                 tt.MouseEnter += new MouseEventHandler(OnEnter);
   23:                 tt.MouseLeave += new EventHandler(OnLeave);
   24:             }
   25:         }
   26:         public void OnEnter(object sender, MouseEventArgs e)
   27:         {
   28:             TextBlock tt = sender as TextBlock;
   29:             strCurrentText = tt.Text;
   30:             tt.Text = "I am in : " + strCurrentText;
   31:         }
   32:         public void OnLeave(object sender, EventArgs e)
   33:         {
   34:             TextBlock tt = sender as TextBlock;
   35:             tt.Text = strCurrentText;
   36:         }
   37:     }
   38: }

 

And here's what the final result looks like:

image

Many thanks to Jeff Paries for catching this one! :)