Skip to content

Commit 02c2ec4

Browse files
authored
Use CommunityToolkit.Mvvm with Dialogs view in demo (#3566)
Also fix any code style issues
1 parent 8585b3e commit 02c2ec4

File tree

1 file changed

+22
-39
lines changed

1 file changed

+22
-39
lines changed

src/MainDemo.Wpf/Domain/DialogsViewModel.cs

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
using System.Diagnostics;
2+
using CommunityToolkit.Mvvm.ComponentModel;
3+
using CommunityToolkit.Mvvm.Input;
24
using MaterialDesignThemes.Wpf;
35

46
namespace MaterialDesignDemo.Domain;
57

6-
public class DialogsViewModel : ViewModelBase
8+
public partial class DialogsViewModel : ObservableObject
79
{
8-
public DialogsViewModel()
9-
{
10-
//Sample 4
11-
OpenSample4DialogCommand = new AnotherCommandImplementation(OpenSample4Dialog);
12-
AcceptSample4DialogCommand = new AnotherCommandImplementation(AcceptSample4Dialog);
13-
CancelSample4DialogCommand = new AnotherCommandImplementation(CancelSample4Dialog);
14-
}
15-
1610
#region SAMPLE 3
1711

18-
public ICommand RunDialogCommand => new AnotherCommandImplementation(ExecuteRunDialog);
19-
20-
public ICommand RunExtendedDialogCommand => new AnotherCommandImplementation(ExecuteRunExtendedDialog);
21-
22-
private async void ExecuteRunDialog(object? _)
12+
[RelayCommand]
13+
private async Task RunDialog()
2314
{
2415
//let's set up a little MVVM, cos that's what the cool kids are doing:
25-
var view = new SampleDialog
16+
object? view = new SampleDialog
2617
{
2718
DataContext = new SampleDialogViewModel()
2819
};
2920

3021
//show the dialog
31-
var result = await DialogHost.Show(view, "RootDialog", null, ClosingEventHandler, ClosedEventHandler);
22+
object? result = await DialogHost.Show(view, "RootDialog", null, ClosingEventHandler, ClosedEventHandler);
3223

3324
//check the result...
3425
Debug.WriteLine("Dialog was closed, the CommandParameter used to close it was: " + (result ?? "NULL"));
@@ -40,16 +31,17 @@ private void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs
4031
private void ClosedEventHandler(object sender, DialogClosedEventArgs eventArgs)
4132
=> Debug.WriteLine("You can intercept the closed event here (1).");
4233

43-
private async void ExecuteRunExtendedDialog(object? _)
34+
[RelayCommand]
35+
private async Task RunExtendedDialog()
4436
{
4537
//let's set up a little MVVM, cos that's what the cool kids are doing:
46-
var view = new SampleDialog
38+
object? view = new SampleDialog
4739
{
4840
DataContext = new SampleDialogViewModel()
4941
};
5042

5143
//show the dialog
52-
var result = await DialogHost.Show(view, "RootDialog", ExtendedOpenedEventHandler, ExtendedClosingEventHandler, ExtendedClosedEventHandler);
44+
object? result = await DialogHost.Show(view, "RootDialog", ExtendedOpenedEventHandler, ExtendedClosingEventHandler, ExtendedClosedEventHandler);
5345

5446
//check the result...
5547
Debug.WriteLine("Dialog was closed, the CommandParameter used to close it was: " + (result ?? "NULL"));
@@ -84,35 +76,26 @@ private void ExtendedClosedEventHandler(object sender, DialogClosedEventArgs eve
8476

8577
#region SAMPLE 4
8678

87-
//pretty much ignore all the stuff provided, and manage everything via custom commands and a binding for .IsOpen
88-
public ICommand OpenSample4DialogCommand { get; }
89-
public ICommand AcceptSample4DialogCommand { get; }
90-
public ICommand CancelSample4DialogCommand { get; }
79+
//pretty much ignore all the stuff provided, and manage everything via custom commands and a binding for .IsOpen
9180

92-
private bool _isSample4DialogOpen;
81+
[ObservableProperty]
9382
private object? _sample4Content;
9483

95-
public bool IsSample4DialogOpen
96-
{
97-
get => _isSample4DialogOpen;
98-
set => SetProperty(ref _isSample4DialogOpen, value);
99-
}
100-
101-
public object? Sample4Content
102-
{
103-
get => _sample4Content;
104-
set => SetProperty(ref _sample4Content, value);
105-
}
106-
107-
private void OpenSample4Dialog(object? _)
84+
[ObservableProperty]
85+
private bool _isSample4DialogOpen;
86+
87+
[RelayCommand]
88+
private void OpenSample4Dialog()
10889
{
10990
Sample4Content = new Sample4Dialog();
11091
IsSample4DialogOpen = true;
11192
}
11293

113-
private void CancelSample4Dialog(object? _) => IsSample4DialogOpen = false;
94+
[RelayCommand]
95+
private void CancelSample4Dialog() => IsSample4DialogOpen = false;
11496

115-
private void AcceptSample4Dialog(object? _)
97+
[RelayCommand]
98+
private void AcceptSample4Dialog()
11699
{
117100
//pretend to do something for 3 seconds, then close
118101
Sample4Content = new SampleProgressDialog();

0 commit comments

Comments
 (0)