11using System ;
22using System . Collections . Generic ;
3+ using System . ComponentModel ;
34using System . Linq ;
5+ using System . Runtime . CompilerServices ;
46using System . Text ;
57using System . Threading . Tasks ;
68using System . Windows . Controls ;
79using System . Windows . Input ;
10+ using System . Windows . Threading ;
811using MaterialDesignThemes . Wpf ;
912
1013namespace MaterialDesignColors . WpfExample . Domain
1114{
12- public class DialogsViewModel
15+ public class DialogsViewModel : INotifyPropertyChanged
1316 {
17+ public DialogsViewModel ( )
18+ {
19+ //Sample 4
20+ OpenSample4DialogCommand = new AnotherCommandImplementation ( OpenSample4Dialog ) ;
21+ AcceptSample4DialogCommand = new AnotherCommandImplementation ( AcceptSample4Dialog ) ;
22+ CancelSample4DialogCommand = new AnotherCommandImplementation ( CancelSample4Dialog ) ;
23+ }
24+
25+ #region SAMPLE 3
26+
1427 public ICommand RunDialogCommand => new AnotherCommandImplementation ( ExecuteRunDialog ) ;
1528
29+ public ICommand RunExtendedDialogCommand => new AnotherCommandImplementation ( ExecuteRunExtendedDialog ) ;
30+
1631 private async void ExecuteRunDialog ( object o )
1732 {
1833 //let's set up a little MVVM, cos that's what the cool kids are doing:
1934 var view = new SampleDialog
2035 {
2136 DataContext = new object ( )
2237 } ;
23-
38+
2439 //show the dialog
2540 var result = await DialogHost . Show ( view , "RootDialog" , ClosingEventHandler ) ;
2641
@@ -32,5 +47,105 @@ private void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs
3247 {
3348 Console . WriteLine ( "You can intercept the closing event, and cancel here." ) ;
3449 }
50+
51+ private async void ExecuteRunExtendedDialog ( object o )
52+ {
53+ //let's set up a little MVVM, cos that's what the cool kids are doing:
54+ var view = new SampleDialog
55+ {
56+ DataContext = new object ( )
57+ } ;
58+
59+ //show the dialog
60+ var result = await DialogHost . Show ( view , "RootDialog" , ExtendedOpenedEventHandler , ExtendedClosingEventHandler ) ;
61+
62+ //check the result...
63+ Console . WriteLine ( "Dialog was closed, the CommandParameter used to close it was: " + ( result ?? "NULL" ) ) ;
64+ }
65+
66+ private void ExtendedOpenedEventHandler ( object sender , DialogOpenedEventArgs eventargs )
67+ {
68+ Console . WriteLine ( "You could intercept the open and affect the dialog using eventArgs.Session." ) ;
69+ }
70+
71+ private void ExtendedClosingEventHandler ( object sender , DialogClosingEventArgs eventArgs )
72+ {
73+ if ( ( bool ) eventArgs . Parameter == false ) return ;
74+
75+ //OK, lets cancel the close...
76+ eventArgs . Cancel ( ) ;
77+
78+ //...now, lets update the "session" with some new content!
79+ eventArgs . Session . UpdateContent ( new SampleProgressDialog ( ) ) ;
80+ //note, you can also grab the session when the dialog opens via the DialogOpenedEventHandler
81+
82+ //lets run a fake operation for 3 seconds then close this baby.
83+ Task . Delay ( TimeSpan . FromSeconds ( 3 ) )
84+ . ContinueWith ( ( t , _ ) => eventArgs . Session . Close ( false ) , null ,
85+ TaskScheduler . FromCurrentSynchronizationContext ( ) ) ;
86+ }
87+
88+ #endregion
89+
90+ #region SAMPLE 4
91+
92+ //pretty much ignore all the stuff provided, and manage everything via custom commands and a binding for .IsOpen
93+ public ICommand OpenSample4DialogCommand { get ; }
94+ public ICommand AcceptSample4DialogCommand { get ; }
95+ public ICommand CancelSample4DialogCommand { get ; }
96+
97+ private bool _isSample4DialogOpen ;
98+ private object _sample4Content ;
99+
100+ public bool IsSample4DialogOpen
101+ {
102+ get { return _isSample4DialogOpen ; }
103+ set
104+ {
105+ if ( _isSample4DialogOpen == value ) return ;
106+ _isSample4DialogOpen = value ;
107+ OnPropertyChanged ( ) ;
108+ }
109+ }
110+
111+ public object Sample4Content
112+ {
113+ get { return _sample4Content ; }
114+ set
115+ {
116+ if ( _sample4Content == value ) return ;
117+ _sample4Content = value ;
118+ OnPropertyChanged ( ) ;
119+ }
120+ }
121+
122+ private void OpenSample4Dialog ( object obj )
123+ {
124+ Sample4Content = new Sample4Dialog ( ) ;
125+ IsSample4DialogOpen = true ;
126+ }
127+
128+ private void CancelSample4Dialog ( object obj )
129+ {
130+ IsSample4DialogOpen = false ;
131+ }
132+
133+ private void AcceptSample4Dialog ( object obj )
134+ {
135+ //pretend to do something for 3 seconds, then close
136+ Sample4Content = new SampleProgressDialog ( ) ;
137+ Task . Delay ( TimeSpan . FromSeconds ( 3 ) )
138+ . ContinueWith ( ( t , _ ) => IsSample4DialogOpen = false , null ,
139+ TaskScheduler . FromCurrentSynchronizationContext ( ) ) ;
140+ }
141+
142+ #endregion
143+
144+ public event PropertyChangedEventHandler PropertyChanged ;
145+
146+ protected virtual void OnPropertyChanged ( [ CallerMemberName ] string propertyName = null )
147+ {
148+ PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
149+ }
35150 }
36151}
0 commit comments