| title | Using Portable Class Library with Model-View-View Model | ||
|---|---|---|---|
| ms.custom | |||
| ms.date | 03/30/2017 | ||
| ms.prod | .net | ||
| ms.reviewer | |||
| ms.suite | |||
| ms.technology | dotnet-standard | ||
| ms.tgt_pltfrm | |||
| ms.topic | article | ||
| dev_langs |
|
||
| helpviewer_keywords |
|
||
| ms.assetid | 41a0b9f8-15a2-431a-bc35-e310b2953b03 | ||
| caps.latest.revision | 17 | ||
| author | mairaw | ||
| ms.author | mairaw | ||
| manager | wpickett | ||
| ms.workload |
|
You can use the .NET Framework Portable Class Library to implement the Model-View-View Model (MVVM) pattern and share assemblies across multiple platforms.
MVVM is an application pattern that isolates the user interface from the underlying business logic. You can implement the model and view model classes in a [!INCLUDEnet_portable] project in [!INCLUDEvs_dev11_long], and then create views that are customized for different platforms. This approach enables you to write the data model and business logic only once, and use that code from .NET Framework, Silverlight, Windows Phone, and [!INCLUDEwin8_appname_long] apps, as shown in the following illustration.
This topic does not provide general information about the MVVM pattern. It only provides information about how to use [!INCLUDEnet_portable] to implement MVVM. For more information about MVVM, see the MVVM Quickstart.
When you target the [!INCLUDEnet_v45], [!INCLUDEnet_win8_profile], Silverlight, or Windows Phone 7.5 for your [!INCLUDEnet_portable] project, the following classes are available for implementing the MVVM pattern:
-
xref:System.Collections.ObjectModel.ObservableCollection%601?displayProperty=nameWithType class
-
xref:System.Collections.ObjectModel.ReadOnlyObservableCollection%601?displayProperty=nameWithType class
-
xref:System.Collections.Specialized.INotifyCollectionChanged?displayProperty=nameWithType class
-
xref:System.Collections.Specialized.NotifyCollectionChangedAction?displayProperty=nameWithType class
-
xref:System.Collections.Specialized.NotifyCollectionChangedEventArgs?displayProperty=nameWithType class
-
xref:System.Collections.Specialized.NotifyCollectionChangedEventHandler?displayProperty=nameWithType class
-
xref:System.ComponentModel.DataErrorsChangedEventArgs?displayProperty=nameWithType class
-
xref:System.ComponentModel.INotifyDataErrorInfo?displayProperty=nameWithType class
-
xref:System.ComponentModel.INotifyPropertyChanged?displayProperty=nameWithType class
-
xref:System.Windows.Input.ICommand?displayProperty=nameWithType class
-
All classes in the xref:System.ComponentModel.DataAnnotations?displayProperty=nameWithType namespace
To implement MVVM, you typically create both the model and the view model in a [!INCLUDEnet_portable] project, because a [!INCLUDEnet_portable] project cannot reference a non-portable project. The model and view model can be in the same project or in separate projects. If you use separate projects, add a reference from the view model project to the model project.
After you compile the model and view model projects, you reference those assemblies in the app that contains the view. If the view interacts only with the view model, you only have to reference the assembly that contains the view model.
The following example shows a simplified model class that could reside in a [!INCLUDEnet_portable] project.
[!code-csharpPortableClassLibraryMVVM#1] [!code-vbPortableClassLibraryMVVM#1]
The following example shows a simple way to populate, retrieve, and update the data in a [!INCLUDEnet_portable] project. In a real app, you would retrieve the data from a source such as a Windows Communication Foundation (WCF) service.
[!code-csharpPortableClassLibraryMVVM#2] [!code-vbPortableClassLibraryMVVM#2]
A base class for view models is frequently added when implementing the MVVM pattern. The following example shows a base class.
[!code-csharpPortableClassLibraryMVVM#3] [!code-vbPortableClassLibraryMVVM#3]
An implementation of the xref:System.Windows.Input.ICommand interface is frequently used with the MVVM pattern. The following example shows an implementation of the xref:System.Windows.Input.ICommand interface.
[!code-csharpPortableClassLibraryMVVM#4] [!code-vbPortableClassLibraryMVVM#4]
The following example shows a simplified view model.
[!code-csharpPortableClassLibraryMVVM#5] [!code-vbPortableClassLibraryMVVM#5]
From a [!INCLUDEnet_v45] app, [!INCLUDEwin8_appname_long] app, Silverlight-based app, or Windows Phone 7.5 app, you can reference the assembly that contains the model and view model projects. You then create a view that interacts with the view model. The following example shows a simplified Windows Presentation Foundation (WPF) app that retrieves and updates data from the view model. You could create similar views in Silverlight, Windows Phone, or [!INCLUDEwin8_appname_long] apps.
[!code-xamlPortableClassLibraryMVVM#6]
