共用方式為


B - Prism Library for Windows Phone

patterns & practices Developer Center

On this page: Download:
About Prism for Windows Phone | Contents of Prism for Windows Phone Library | Microsoft.Practices.Prism Namespace | Microsoft.Practices.Prism.Commands Namespace | Microsoft.Practices.Prism.Events Namespace | Microsoft.Practices.Prism.ViewModel Namespace | Microsoft.Practices.Prism.Interactivity Namespace | Microsoft.Practices.Prism.Interactivity.InteractionRequest Namespace Download code samples
Download book as PDF

Prism is a free library from the Microsoft patterns & practices group. The components in this library can help developers build applications for Windows® Presentation Foundation (WPF), Microsoft Silverlight® browser plug-in, and the Windows Phone platform that are easier to maintain and update as requirements change.

Prism was originally built to support composite application scenarios, where you might typically have a shell application and modules that contribute pieces to the overall application. Although Prism supports this application style, most of the library components offered by Prism can be useful in building any WPF or Silverlight application. For example, Prism offers components to support the Model-View-ViewModel (MVVM) pattern as well as pieces to provide loosely coupled communication between parts of an application.

Although the use of a display shell is typically not appropriate in a Windows Phone application, many of the other components within Prism are useful when building Silverlight applications for Windows Phone. For example, you can use the DelegateCommand provided by Prism to avoid the requirement to implement event handlers in the code-behind of your views when using the MVVM pattern for your applications.

Prism includes a small library known as the Prism Library for Windows Phone, which contains a subset of the main Prism Library features specifically aimed at helping developers implement solutions to common issues found in developing applications for Windows Phone. The library includes classes to help developers implement commands, navigation, observable object notifications, data template selection, interaction with notifications, interaction with the application bar, and more.

This appendix provides an overview of the Prism Library for Windows Phone, which is used in the Tailspin application for Windows Phone discussed in this guide. For more information and to download Prism, see the Prism home page on the MSDN® developer program website. To provide feedback, get assistance, or download additional content, visit the Prism community site on CodePlex.

About Prism for Windows Phone

Windows Phone applications implemented using Silverlight are naturally suited to the MVVM pattern, which discourages the use of code-behind in the views in favor of handling events and activity in the view model. However, many common scenarios—such as binding commands to interface objects, linking methods to application bar buttons, notifying of changes to object properties, and detecting changes to text-based controls in the view—are challenging to accomplish without using code-behind. The helper classes and components in Prism Library for Windows Phone are specially designed to simplify these tasks in Silverlight applications created for Windows Phone.

In addition, the Prism Library for Windows Phone includes helper classes for publishing and subscribing to events, displaying notification messages, and selecting data templates at run time. Many of these components and helper classes are used in the Tailspin.PhoneClient project. You can download the complete Windows Phone Tailspin Surveys application for use in conjunction with this guide from the Microsoft Download Center.

The Prism Library for Windows Phone is provided as source code in the two projects, Microsoft.Practices.Prism and Microsoft.Practices.Prism.Interactivity, within the Tailspin solution. These solutions implement the Prism Library for Windows Phone library, and there is also a set of tests to help you explore the use of the classes in the library. These tests can also be used if you extend or modify the source code to meet your own specific requirements.

Contents of Prism for Windows Phone Library

The Prism Library for Windows Phone contains several namespaces that subdivide the artifacts:

  • Microsoft.Practices.Prism. This namespace contains classes concerned with detecting and reacting to change events for properties and objects.
  • Microsoft.Practices.Prism.Commands. This namespace contains classes concerned with binding commands to user interface (UI) objects without requiring the use of code-behind in the view, and composing multiple commands.
  • Microsoft.Practices.Prism.Events. This namespace contains classes concerned with subscribing to events and with publishing events on the publisher thread, UI thread, or a background thread.
  • Microsoft.Practices.Prism.ViewModel. This namespace contains classes that help support implementation of the view model portion of the MVVM pattern, such as displaying a template at run time and simplifying the implementation of property change notification.
  • Microsoft.Practices.Prism.Interactivity. This namespace contains classes and custom behaviors concerned with handling interaction and navigation for application bar buttons and with updating the values of view model properties bound to text and password controls.
  • Microsoft.Practices.Prism.Interactivity.InteractionRequest. This namespace contains classes concerned with displaying notifications to users.

The following tables list the main classes in these namespaces and provide a brief description of their usage. They do not include all the classes in each namespace; they list only those that implement the primary functions of the library. For a full reference of all the Prism namespaces, see "Prism (Composite Client Application Guidance)" on MSDN.

Microsoft.Practices.Prism Namespace

The following table lists the main components in the Microsoft.Practices.Prism namespace. These classes are not used in the Tailspin application.

Class

Description

ObservableObject<T>

A class that wraps an object so that other classes can be notified of change events. Typically, this class is set as a dependency property on dependency objects and allows other classes to observe any changes in the property values. This class is required because, in Silverlight, it is not possible to receive change notifications for dependency properties that you do not own.

ExtensionMethods

(for List<T>)

This class adds the RemoveAll method to List<T>. The method removes the all the elements that match the conditions defined by the specified predicate.

For more information about using the ObservableObject class, see the section "Sharing Data Between Multiple Regions," in "Prism (Composite Application Guidance)" on MSDN.

Microsoft.Practices.Prism.Commands Namespace

The following table lists the main components in the Microsoft.Practices.Prism.Commands namespace.

Class

Description

Usage in Tailspin application

CompositeCommand

This class composes one or more ICommand implementation instances. It forwards to all the registered commands, and returns true if all the commands return true.

This class is not used in the Tailspin application.

DelegateCommand

This class is an implementation of ICommand. Its delegates can be attached to access the Execute and CanExecute methods.

Used in most of the view models for associating commands with actions that execute in response to a command being fired. For more information, see the section, "Commands," in Chapter 2, "Building the Mobile Client."

For more information about using the DelegateCommand and CompositeCommand classes, see the section, "Commands" in Chapter 5, "Implementing the MVVM Pattern," in "Prism (Composite Application Guidance)" on MSDN.

Microsoft.Practices.Prism.Events Namespace

The following table lists the main components in the Microsoft.Practices.Prism.Events namespace. Classes in this namespace support decoupled communication between pieces of an application, such as between two view models. These classes are not used in the Tailspin application.

Class

Description

CompositePresentationEvent

A class that manages publication and subscription to events.

SubscriptionToken

The subscription token returned from an event subscription method. This class provides methods to compare tokens.

EventSubscription

This class provides a method for retrieving a Delegate that executes an Action depending on the value of a second filter predicate that returns true if the action should execute.

BackgroundEventSubscription

This class extends EventSubscription to invoke the Action delegate on a background thread.

DispatcherEventSubscription

This class extends EventSubscription to invoke the Action delegate in a specific Dispatcher instance.

EventAggregator

This class implements a service that stores event references and exposes the GetEvent method to retrieve a specific event type.

For more information about publishing and subscribing to events, see Chapter 9, "Communicating between Loosely Coupled Components," in "Prism (Composite Application Guidance)" on MSDN.

Microsoft.Practices.Prism.ViewModel Namespace

The following table lists the main components in the Microsoft.Practices.Prism.ViewModel namespace.

Class

Description

Usage in Tailspin application

DataTemplateSelector

This class implements a custom ContentControl that changes its ContentTemplate based on the content it is presenting. To determine the template it must use for the new content, the control retrieves it from its resources using the name for the type of the new content as the key.

Used in the TakeSurveyView view to select the appropriate question view, depending on the type of question. For more information, see the section, "Data Binding and the Pivot Control," in Chapter 2, "Building the Mobile Client."

NotificationObject

This is a base class for items that support property notification. It provides basic support for implementing the INotifyPropertyChanged interface and for marshaling execution to the UI thread.

Used in the ViewModel, QuestionViewModel, QuestionOption, and QuestionAnswer classes. For more information, see the section, "Displaying Data," in Chapter 2, "Building the Mobile Client."

For more information about using the DataTemplateSelector class, see "MVVM QuickStart" in "Prism (Composite Application Guidance)" on MSDN.

Microsoft.Practices.Prism.Interactivity Namespace

The following table lists the main components in the Microsoft.Practices.Prism.Interactivity namespace.

Class

Description

Usage in Tailspin application

ApplicationBarButtonCommand

This class implements a behavior that associates a command with an application bar button.

Used in the SurveyListView, TakeSurveyView, AppSettingsView, and FilterSettingsView views to bind the click events for application bar buttons to methods in the view models. For more information, see the section, "Commands," in Chapter 2, "Building the Mobile Client."

ApplicationBarButtonNavigation

This class implements a behavior that subscribes to an application bar button click event and navigates to a specified page.

This class is not used in the Tailspin application.

ApplicationBarExtensions

This class provides the FindButton method to find a button on the phone's application bar.

The FindButton method is used by the ApplicationBarButtonCommand class to find a button by name.

UpdatePasswordBindingOn‌PropertyChanged

This class implements a behavior that updates the source of a binding on a password box as the text changes. By default in Silverlight, the bound property is only updated when the control loses focus.

Used in the AppSettingsView view to ensure that the user's password input is updated in the view model even if the password box does not lose focus. For more information, see the section, "Displaying Data," in Chapter 2, "Building the Mobile Client."

UpdateTextBindingOn‌PropertyChanged

This class implements a behavior that updates the source of a binding on a text box as the text changes. By default in Silverlight, the bound property is only updated when the control loses focus.

Used in the AppSettingsView and OpenQuestionView views to ensure that the user's text input is updated in the view model even if the text box does not lose focus. For more information, see the section, "Displaying Data," in Chapter 2, "Building the Mobile Client."

Microsoft.Practices.Prism.Interactivity.InteractionRequest Namespace

The following table lists the main components in the Microsoft.Practices.Prism.Interactivity.InteractionRequest namespace.

Class

Description

Usage in Tailspin application

InteractionRequest

This class represents a request for user interaction. View models can expose interaction request objects through properties and raise them when user interaction is required so that views associated with the view models can materialize the user interaction using an appropriate mechanism.

Used in the SurveyListViewModel, AppSettingsViewModel, and FilterSettingsViewModel view models to generate interaction requests that display notification messages. For more information, see the section, "User Interface Notifications," in Chapter 2, "Building the Mobile Client."

MessageBoxAction

This class displays a message box as a result of an interaction request.

Used in the SurveyListView, AppSettingsView, and FilterSettingsView views to display message boxes. For more information, see the section, "User Interface Notifications," in Chapter 2, "Building the Mobile Client."

ToastPopupAction

This class displays a pop-up toast item with specified content as a result of an interaction request. After a short period, it removes the pop-up window.

Used in the SurveyListView view to display information about the most recent synchronization with the remote service. For more information, see the section, "User Interface Notifications," in Chapter 2, "Building the Mobile Client."

For more information about interaction requests and displaying notifications, see Chapter 6, "Advanced MVVM Scenarios" in "Prism (Composite Application Guidance)" on MSDN.

These and all links in this book are accessible from the book's online bibliography. You can find the bibliography on MSDN at: https://msdn.microsoft.com/en-us/library/gg490786.aspx.

Next Topic | Previous Topic | Home

Last built: May 25, 2012