forked from MaterialDesignInXAML/MaterialDesignInXamlToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogsViewModel.cs
More file actions
122 lines (96 loc) · 4.5 KB
/
DialogsViewModel.cs
File metadata and controls
122 lines (96 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Input;
using MaterialDesignThemes.Wpf;
namespace MaterialDesignDemo.Domain
{
public class DialogsViewModel : ViewModelBase
{
public DialogsViewModel()
{
//Sample 4
OpenSample4DialogCommand = new AnotherCommandImplementation(OpenSample4Dialog);
AcceptSample4DialogCommand = new AnotherCommandImplementation(AcceptSample4Dialog);
CancelSample4DialogCommand = new AnotherCommandImplementation(CancelSample4Dialog);
}
#region SAMPLE 3
public ICommand RunDialogCommand => new AnotherCommandImplementation(ExecuteRunDialog);
public ICommand RunExtendedDialogCommand => new AnotherCommandImplementation(ExecuteRunExtendedDialog);
private async void ExecuteRunDialog(object o)
{
//let's set up a little MVVM, cos that's what the cool kids are doing:
var view = new SampleDialog
{
DataContext = new SampleDialogViewModel()
};
//show the dialog
var result = await DialogHost.Show(view, "RootDialog", ClosingEventHandler);
//check the result...
Debug.WriteLine("Dialog was closed, the CommandParameter used to close it was: " + (result ?? "NULL"));
}
private void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
=> Debug.WriteLine("You can intercept the closing event, and cancel here.");
private async void ExecuteRunExtendedDialog(object o)
{
//let's set up a little MVVM, cos that's what the cool kids are doing:
var view = new SampleDialog
{
DataContext = new SampleDialogViewModel()
};
//show the dialog
var result = await DialogHost.Show(view, "RootDialog", ExtendedOpenedEventHandler, ExtendedClosingEventHandler);
//check the result...
Debug.WriteLine("Dialog was closed, the CommandParameter used to close it was: " + (result ?? "NULL"));
}
private void ExtendedOpenedEventHandler(object sender, DialogOpenedEventArgs eventargs)
=> Debug.WriteLine("You could intercept the open and affect the dialog using eventArgs.Session.");
private void ExtendedClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
{
if (eventArgs.Parameter is bool parameter &&
parameter == false) return;
//OK, lets cancel the close...
eventArgs.Cancel();
//...now, lets update the "session" with some new content!
eventArgs.Session.UpdateContent(new SampleProgressDialog());
//note, you can also grab the session when the dialog opens via the DialogOpenedEventHandler
//lets run a fake operation for 3 seconds then close this baby.
Task.Delay(TimeSpan.FromSeconds(3))
.ContinueWith((t, _) => eventArgs.Session.Close(false), null,
TaskScheduler.FromCurrentSynchronizationContext());
}
#endregion
#region SAMPLE 4
//pretty much ignore all the stuff provided, and manage everything via custom commands and a binding for .IsOpen
public ICommand OpenSample4DialogCommand { get; }
public ICommand AcceptSample4DialogCommand { get; }
public ICommand CancelSample4DialogCommand { get; }
private bool _isSample4DialogOpen;
private object? _sample4Content;
public bool IsSample4DialogOpen
{
get => _isSample4DialogOpen;
set => SetProperty(ref _isSample4DialogOpen, value);
}
public object? Sample4Content
{
get => _sample4Content;
set => SetProperty(ref _sample4Content, value);
}
private void OpenSample4Dialog(object obj)
{
Sample4Content = new Sample4Dialog();
IsSample4DialogOpen = true;
}
private void CancelSample4Dialog(object obj) => IsSample4DialogOpen = false;
private void AcceptSample4Dialog(object obj)
{
//pretend to do something for 3 seconds, then close
Sample4Content = new SampleProgressDialog();
Task.Delay(TimeSpan.FromSeconds(3))
.ContinueWith((t, _) => IsSample4DialogOpen = false, null,
TaskScheduler.FromCurrentSynchronizationContext());
}
#endregion
}
}