CHAPTER 14 EXERCISE SOLUTIONS

Exercise 1 Solution

You can use MVVM to separate views (user interface elements) from view models (functionality and business logic). This gives your applications a much better and more maintainable structure, which is particularly useful in large applications, such as those that have large teams of developers and designers.

Exercise 2 Solution

Modify the code for ButtonCommandHelper.cs so that it looks like the following:

public static class ButtonCommandHelper
{
   …

   public static readonly DependencyProperty
      CommandParameterProperty =
         DependencyProperty.RegisterAttached(
            “CommandParameter”,
            typeof(object),
            typeof(ButtonCommandHelper),
            new PropertyMetadata(null));

   public static object GetCommandParameter(
      ButtonBase button)
   {
      return button.GetValue(CommandParameterProperty);
   }

   public static void SetCommandParameter(
      ButtonBase button, object commandParameter)
   {
      button.SetValue(CommandParameterProperty, commandParameter);
   }

   …

   private static void OnButtonBaseClick(object sender, RoutedEventArgs args)
   {
      var button = sender as ButtonBase;
      var command = GetCommand(button);

      var commandParameter = GetCommandParameter(button);
      if (command != null && command.CanExecute(commandParameter))
      {
         command.Execute(commandParameter);
      }
   }
}

Exercise 3 Solution

Most unit test frameworks throw exceptions when tests fail, although in practice these exceptions are handled by the framework and converted into user-friendly output you can peruse.

Get Beginning Windows® Phone 7 Application Development: Building Windows® Phone Applications Using Silverlight® and XNA® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.