Skip to content

Commit bcbe6fc

Browse files
committed
Removed repeated code, minor fixes
1 parent 724c4cb commit bcbe6fc

File tree

4 files changed

+40
-117
lines changed

4 files changed

+40
-117
lines changed

samples/MvvmSampleUwp/MvvmSampleUwp/Assets/docs/AsyncRelayCommand.md

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,52 +21,7 @@ The [AsyncRelayCommand](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.
2121

2222
## Working with asynchronous commands
2323

24-
Let's imagine a scenario similar to the one described in the `RelayCommand` sample, but a command executing an asynchronous operation:
25-
26-
```csharp
27-
public class MyViewModel : ObservableObject
28-
{
29-
public MyViewModel()
30-
{
31-
DownloadTextCommand = new AsyncRelayCommand(DownloadText);
32-
}
33-
34-
public IAsyncRelayCommand DownloadTextCommand { get; }
35-
36-
private Task<string> DownloadText()
37-
{
38-
return WebService.LoadMyTextAsync();
39-
}
40-
}
41-
```
42-
43-
With the related UI code:
44-
45-
```xml
46-
<Page
47-
x:Class="MyApp.Views.MyPage"
48-
xmlns:viewModels="using:MyApp.ViewModels">
49-
<Page.DataContext>
50-
<viewModels:MyViewModel x:Name="ViewModel"/>
51-
</Page.DataContext>
52-
53-
<StackPanel Spacing="8">
54-
<TextBlock>
55-
<Run Text="Task status:"/>
56-
<Run Text="{x:Bind ViewModel.DownloadTextCommand.ExecutionTask.Status, Mode=OneWay}"/>
57-
<LineBreak/>
58-
<Run Text="Result:"/>
59-
<Run
60-
xmlns:ex="using:Microsoft.Toolkit.Extensions"
61-
Text="{x:Bind ex:TaskExtensions.GetResultOrDefault(ViewModel.DownloadTextCommand.ExecutionTask), Mode=OneWay}"/>
62-
</TextBlock>
63-
<Button
64-
Content="Click me!"
65-
Command="{x:Bind ViewModel.DownloadTextCommand}"/>
66-
<ProgressRing IsActive="{x:Bind ViewModel.DownloadTextCommand.IsRunning, Mode=OneWay}"/>
67-
</StackPanel>
68-
</Page>
69-
```
24+
Let's imagine a scenario similar to the one described in the `RelayCommand` sample, but a command executing an asynchronous operation.
7025

7126
Upon clicking the `Button`, the command is invoked, and the `ExecutionTask` updated. When the operation completes, the property raises a notification which is reflected in the UI. In this case, both the task status and the current result of the task are displayed. Note that to show the result of the task, it is necessary to use the `TaskExtensions.GetResultOrDefault` method - this provides access to the result of a task that has not yet completed without blocking the thread (and possibly causing a deadlock).
7227

samples/MvvmSampleUwp/MvvmSampleUwp/Assets/docs/ObservableObject.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class ObservableUser : ObservableObject
5353
public string Name
5454
{
5555
get => user.Name;
56-
set => Set(() => user.Name, value);
56+
set => SetProperty(() => user.Name, value);
5757
}
5858
}
5959
```

samples/MvvmSampleUwp/MvvmSampleUwp/Assets/docs/RelayCommand.md

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,7 @@ The [RelayCommand](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.mvvm.
2121

2222
## Working with `ICommand`
2323

24-
The following shows how to set up a simple command:
25-
26-
```csharp
27-
public class MyViewModel : ObservableObject
28-
{
29-
public MyViewModel()
30-
{
31-
IncrementCounterCommand = new RelayCommand(IncrementCounter);
32-
}
33-
34-
private int counter;
35-
36-
public int Counter
37-
{
38-
get => counter;
39-
private set => SetProperty(ref counter, value);
40-
}
41-
42-
public ICommand IncrementCounterCommand { get; }
43-
44-
private void IncrementCounter() => Counter++;
45-
}
46-
```
47-
48-
And the relative UI could then be (using WinUI XAML):
49-
50-
```xml
51-
<Page
52-
x:Class="MyApp.Views.MyPage"
53-
xmlns:viewModels="using:MyApp.ViewModels">
54-
<Page.DataContext>
55-
<viewModels:MyViewModel x:Name="ViewModel"/>
56-
</Page.DataContext>
57-
58-
<StackPanel Spacing="8">
59-
<TextBlock Text="{x:Bind ViewModel.Counter, Mode=OneWay}"/>
60-
<Button
61-
Content="Click me!"
62-
Command="{x:Bind ViewModel.IncrementCounterCommand}"/>
63-
</StackPanel>
64-
</Page>
65-
```
66-
67-
The `Button` binds to the `ICommand` in the viewmodel, which wraps the private `IncrementCounter` method. The `TextBlock` displays the value of the `Counter` property and is updated every time the property value changes.
24+
The following sample shows how to set up a simple command using `RelayCommand` to abstract a method in the viewmodel. We also have a property raising change notifications through the `SetProperty` method inherited from `ObservableObject`. The UI has a `Button` control binding to the `ICommand` in the viewmodel, and a `TextBlock` that displays the value of the `Counter` property.
6825

6926
## Sample Code
7027

samples/MvvmSampleUwp/MvvmSampleUwp/Views/RelayCommandPage.xaml

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,50 @@
3636
</StackPanel>
3737
</controls:InteractiveSample.Content>
3838
<controls:InteractiveSample.XamlCode>
39-
&lt;StackPanel Spacing="8">
40-
&lt;TextBlock Text="{x:Bind ViewModel.Counter, Mode=OneWay}"/>
41-
&lt;Button
42-
Content="Click me!"
43-
Command="{x:Bind ViewModel.IncrementCounterCommand}"/>
44-
&lt;/StackPanel>
39+
&lt;Page
40+
x:Class="MyApp.Views.MyPage"
41+
xmlns:viewModels="using:MyApp.ViewModels">
42+
&lt;Page.DataContext>
43+
&lt;viewModels:MyViewModel x:Name="ViewModel"/>
44+
&lt;/Page.DataContext>
45+
46+
&lt;StackPanel Spacing="8">
47+
&lt;TextBlock Text="{x:Bind ViewModel.Counter, Mode=OneWay}"/>
48+
&lt;Button
49+
Content="Click me!"
50+
Command="{x:Bind ViewModel.IncrementCounterCommand}"/>
51+
&lt;/StackPanel>
52+
&lt;/Page>
4553
</controls:InteractiveSample.XamlCode>
4654
<controls:InteractiveSample.CSharpCode>
47-
public MyViewModel()
55+
public class MyViewModel : ObservableObject
4856
{
49-
IncrementCounterCommand = new RelayCommand(IncrementCounter);
50-
}
57+
public MyViewModel()
58+
{
59+
IncrementCounterCommand = new RelayCommand(IncrementCounter);
60+
}
5161

52-
/// &lt;summary>
53-
/// Gets the &lt;see cref="ICommand"/> responsible for incrementing &lt;see cref="Counter"/>.
54-
/// &lt;/summary>
55-
public ICommand IncrementCounterCommand { get; }
62+
/// &lt;summary>
63+
/// Gets the &lt;see cref="ICommand"/> responsible for incrementing &lt;see cref="Counter"/>.
64+
/// &lt;/summary>
65+
public ICommand IncrementCounterCommand { get; }
5666

57-
private int counter;
67+
private int counter;
5868

59-
/// &lt;summary>
60-
/// Gets the current value of the counter.
61-
/// &lt;/summary>
62-
public int Counter
63-
{
64-
get => counter;
65-
private set => SetProperty(ref counter, value);
66-
}
69+
/// &lt;summary>
70+
/// Gets the current value of the counter.
71+
/// &lt;/summary>
72+
public int Counter
73+
{
74+
get => counter;
75+
private set => SetProperty(ref counter, value);
76+
}
6777

68-
/// &lt;summary>
69-
/// Increments &lt;see cref="Counter"/>.
70-
/// &lt;/summary>
71-
private void IncrementCounter() => Counter++;
78+
/// &lt;summary>
79+
/// Increments &lt;see cref="Counter"/>.
80+
/// &lt;/summary>
81+
private void IncrementCounter() => Counter++;
82+
}
7283
</controls:InteractiveSample.CSharpCode>
7384
</controls:InteractiveSample>
7485
</StackPanel>

0 commit comments

Comments
 (0)