Skip to content

Commit e9a24cc

Browse files
authored
Merge pull request #18 from mrlacey/i17-MigrateMvvmBasic
Create MigratingFromMvvmBasic.md
2 parents 563f422 + e652830 commit e9a24cc

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

docs/mvvm/MigratingFromMvvmBasic.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
title: Migrating from MVVM Basic
3+
author: mrlacey
4+
description: This article describes how to migrate MvvmLight solutions to the Windows Community Toolkit MVVM framework.
5+
keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, mvvm, mvvmbasic, MVVM Basic, net core, net standard, Windows Template Studio, WinTS
6+
dev_langs:
7+
- csharp
8+
- vb
9+
---
10+
11+
# Migrating from MVVM Basic
12+
13+
This article explains how to migrate apps built with the [MVVM](https://github.com/microsoft/WindowsTemplateStudio/blob/dev/docs/UWP/frameworks/mvvmbasic.md) Basic option in [Windows Template Studio](https://marketplace.visualstudio.com/items?itemName=WASTeamAccount.WindowsTemplateStudio) to use the Toolkit MVVM library instead. It applies to both UWP and WPF apps created with Windows Template Studio.
14+
15+
This article focuses exclusively on migration and does not cover how to use the additional functionality that the library provides.
16+
17+
## Installing the WCT MVVM Toolkit
18+
19+
To use the Windows Community Toolkit MVVM framework, you must install the NuGet package into your existing application.
20+
21+
### Install via .NET CLI
22+
23+
```
24+
dotnet add package Microsoft.Toolkit.Mvvm --version x.x.x
25+
```
26+
27+
### Install via PackageReference
28+
29+
```
30+
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="x.x.x" />
31+
```
32+
33+
## Updating a project
34+
35+
There are four steps to migrate the code generated by Windows Template Studio.
36+
37+
1. Delete old files.
38+
2. Replace use of `Observable`.
39+
3. Add new namespace references.
40+
4. Update methods with different names.
41+
42+
### 1. Delete old files
43+
44+
MVVM Basic is comprised of two files
45+
46+
```csharp
47+
\Helpers\Observable.cs
48+
\Helpers\RelayCommand.cs
49+
```
50+
```vb
51+
\Helpers\Observable.vb
52+
\Helpers\RelayCommand.vb
53+
```
54+
55+
Delete both of these files.
56+
57+
If you try and build the project at this point you will see lots of errors. These can be useful for identifying files that require changes.
58+
59+
### 2. Replace Use of `Observable`
60+
61+
The `Observable` class was used as a base class for ViewModels. The MVVM Toolkit contains a similar class with additional functionality that is called `ObservableObject`.
62+
63+
Change all classes that previously inherited from `Observable` to inherit from `ObservableObject`.
64+
65+
For example
66+
67+
```csharp
68+
public class MainViewModel : Observable
69+
```
70+
```vb
71+
Public Class MainViewModel
72+
Inherits Observable
73+
```
74+
75+
will become
76+
77+
```csharp
78+
public class MainViewModel : ObservableObject
79+
```
80+
```vb
81+
Public Class MainViewModel
82+
Inherits ObservableObject
83+
```
84+
85+
### 3. Add new namespace references
86+
87+
Add a reference to the `Microsoft.Toolkit.Mvvm.ComponentMode` namespace in all files where there is a reference to `ObservableObject`.
88+
89+
You can either add the appropriate directive manually, of move the cursor to the `ObservableObject` and press `Ctrl+.` to access the Quick Action menu to add this for you.
90+
91+
```csharp
92+
using Microsoft.Toolkit.Mvvm.ComponentModel;
93+
```
94+
```vb
95+
Imports Microsoft.Toolkit.Mvvm.ComponentModel
96+
```
97+
98+
Add a reference to the `Microsoft.Toolkit.Mvvm.Input` namespace in all files where there is a reference to `RelayCommand`.
99+
100+
You can either add the appropriate directive manually, of move the cursor to the `RelayCommand` and press `Ctrl+.` to access the Quick Action menu to add this for you.
101+
102+
```csharp
103+
using Microsoft.Toolkit.Mvvm.Input;
104+
```
105+
```vb
106+
Imports Microsoft.Toolkit.Mvvm.Input
107+
```
108+
109+
### 4. Update methods with different names
110+
111+
There are two methods that must be updated to allow for different names for the same functionality.
112+
113+
All calls to `Observable.Set` must be replaced with calls to `ObservableObject.SetProperty`.
114+
115+
So,
116+
117+
```csharp
118+
set { Set(ref _elementTheme, value); }
119+
```
120+
```vb
121+
Set
122+
[Set](_elementTheme, Value)
123+
End Set
124+
```
125+
126+
will become
127+
128+
```csharp
129+
set { SetProperty(ref _elementTheme, value); }
130+
```
131+
```vb
132+
Set
133+
SetProperty(_elementTheme, Value)
134+
End Set
135+
```
136+
137+
All calls to `RelayCommand.OnCanExecuteChanged` must be replaced with calls to `RelayCommand.NotifyCanExecuteChanged`.
138+
139+
So,
140+
141+
```csharp
142+
(UndoCommand as RelayCommand)?.OnCanExecuteChanged();
143+
```
144+
```vb
145+
Dim undo = TryCast(UndoCommand, RelayCommand)
146+
undo?.OnCanExecuteChanged()
147+
```
148+
149+
will become
150+
151+
```csharp
152+
(UndoCommand as RelayCommand)?.NotifyCanExecuteChanged();
153+
```
154+
```vb
155+
Dim undo = TryCast(UndoCommand, RelayCommand)
156+
undo?.NotifyCanExecuteChanged()
157+
```
158+
159+
The app should now work with the same functionality as before.

0 commit comments

Comments
 (0)