Поделиться через


Multi-Targeting

This topic helps you understand what multi-targeting is and its advantages and disadvantages. Multi-targeted code is targeting two different platforms with largely the same code-base. This allows binaries targeting two different technologies to be produced while keeping the code as much the same as possible. In this case, the technologies that this topic describes are Windows Presentation Foundation (WPF) and Silverlight.

Goal and Benefits

WPF and Silverlight platforms are very similar, but they do not have binary compatibility; this means that an assembly compiled for one platform cannot execute on the other platform. You may want to target some or all of your application on WPF and Silverlight for a number of reasons.

In this case, the Composite Application Guidance for WPF and Silverlight is targeting WPF and Silverlight to give you the flexibility to create an application using the latest Microsoft technology for desktop and browsers. Supporting multi-targeted applications is primarily about implementing the patterns and infrastructure that maximize the possibility for sharing code and components between the two environments, and for allowing an application to integrate environment-specific functionality so that it can take full advantage of desktop or browser-specific features. By creating your multi-targeting composite application using the Composite Application Guidance, you can reuse source code across WPF and Silverlight applications.

Out of Scope

Silverlight and WPF are not binary compatible, so the focus of this guidance is only on reuse of source code.

Multi-Targeting Scenarios

The primary scenario is for applications that deliver both a feature-rich desktop experience and a wide-reach browser experience. In this scenario, you may want to develop an application that has the same features and workflow on WPF and Silverlight or one that offers different features and workflows. The following are some applications of multi-targeting:

  • You can provide users with a full-featured application while they are in the office and a scaled, browser-based version for when they are traveling.
  • You can provide internal users with a desktop-based application and external customers or partners with a browser-hosted application.

For example, a business may have both a call-center application for customers who want to place their orders over the phone and an online order application for customers who want to place their orders online. However, the forms are not the exactly the same. The call-center desktop order form offers more information and expanded functionality than the online order form. However, because they accomplish similar things there will be certain parts of the order form and business logic that can be reused across both scenarios.

Service-oriented applications are easier to multi-target because Silverlight is inherently service-oriented. Silverlight does not have support for local storage or database access because of its reduced feature set and security restrictions. Additionally, connected applications are also easier to multi-target because of Silverlight's connected nature.

Multi-Targeted Considerations

By making your solution multi-targetable, you should also consider the following:

  • Silverlight is an online-only model. You cannot use Silverlight offline. This limits you to certain functional scenarios.
  • Silverlight offers limited storage on the local client computer in isolated storage.
  • You may lose simplicity and readability of code in your multi-targetable solution. Because some features of WPF are not available in Silverlight, you will need to work around these issues and your code may not be as elegant or readable.
  • Silverlight applications execute in a secure sandbox, so there are a several things that you cannot do outside of the browser, such as running devices, interacting with other running programs, reading/writing to the file system without the user's approval, or accessing other local resources.
  • Silverlight supports only asynchronous communications, so you cannot multi-target applications that use synchronous communications.

Multi-Targeted Elements

Typically there is a significant amount of code that is unrelated to the actual presentation technology. Because of the very close nature of the Silverlight and .NET Framework runtimes, the bulk of this code can be shared between both technologies. This also encourages heavy use of Separated Presentation patterns to isolate the logic of the presentation from the actual visual presentation, to help maximize the separation between user interface (UI) and non-UI code. Usually, you can multi-target the following source code elements:

  • Presenters, if the logic is largely the same across the platforms
  • Controllers
  • Models
  • Services
  • Unit tests
  • Simple views, if the XAML used is supported by both Silverlight and WPF. If a view consists of only basic controls and simple data binding, it is possible that it can also be shared between WPF and Silverlight.

Silverlight's API is largely a subset of the .NET Framework's API, so it often makes sense to develop your application against this smaller API to reduce the change of using a feature not available for both platforms.

Because of the differences between the XAML in Silverlight and WPF, the following elements are harder to reuse:

  • Complex views (XAML)
  • Controls
  • Styling
  • Animation

More Information

For information about creating multi-targeted applications in WPF and Silverlight, see the following topics:

For background information about concepts that are important to understanding the Composite Application Guidance, see the following topics:

Home page on MSDN | Community site