O'Reilly logo

Beginning Windows® Phone 7 Application Development: Building Windows® Phone Applications Using Silverlight® and XNA® by Robert Fonseca-Ensor, Karli Watson, Nick Lecrenski

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required