@@ -67,16 +67,16 @@ public class DialogHost : ContentControl
67
67
private Grid _contentCoverGrid ;
68
68
private DialogSession _session ;
69
69
private DialogOpenedEventHandler _attachedDialogOpenedEventHandler ;
70
- private DialogClosingEventHandler _attachedDialogClosingEventHandler ;
70
+ private DialogClosingEventHandler _attachedDialogClosingEventHandler ;
71
71
private object _closeDialogExecutionParameter ;
72
- private IInputElement _restoreFocusDialogClose ;
72
+ private IInputElement _restoreFocusDialogClose ;
73
73
private IInputElement _restoreFocusWindowReactivation ;
74
74
private Action _currentSnackbarMessageQueueUnPauseAction = null ;
75
75
private Action _closeCleanUp = ( ) => { } ;
76
76
77
77
static DialogHost ( )
78
78
{
79
- DefaultStyleKeyProperty . OverrideMetadata ( typeof ( DialogHost ) , new FrameworkPropertyMetadata ( typeof ( DialogHost ) ) ) ;
79
+ DefaultStyleKeyProperty . OverrideMetadata ( typeof ( DialogHost ) , new FrameworkPropertyMetadata ( typeof ( DialogHost ) ) ) ;
80
80
}
81
81
82
82
#region .Show overloads
@@ -158,7 +158,7 @@ public static Task<object> Show(object content, object dialogIdentifier, DialogO
158
158
public static Task < object > Show ( object content , object dialogIdentifier , DialogClosingEventHandler closingEventHandler )
159
159
{
160
160
return Show ( content , dialogIdentifier , null , closingEventHandler ) ;
161
- }
161
+ }
162
162
163
163
/// <summary>
164
164
/// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
@@ -218,7 +218,7 @@ public DialogHost()
218
218
Unloaded += OnUnloaded ;
219
219
220
220
CommandBindings . Add ( new CommandBinding ( CloseDialogCommand , CloseDialogHandler , CloseDialogCanExecute ) ) ;
221
- CommandBindings . Add ( new CommandBinding ( OpenDialogCommand , OpenDialogHandler ) ) ;
221
+ CommandBindings . Add ( new CommandBinding ( OpenDialogCommand , OpenDialogHandler ) ) ;
222
222
}
223
223
224
224
public static readonly DependencyProperty IdentifierProperty = DependencyProperty . Register (
@@ -238,7 +238,7 @@ public object Identifier
238
238
239
239
private static void IsOpenPropertyChangedCallback ( DependencyObject dependencyObject , DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs )
240
240
{
241
- var dialogHost = ( DialogHost ) dependencyObject ;
241
+ var dialogHost = ( DialogHost ) dependencyObject ;
242
242
243
243
if ( dialogHost . _popupContentControl != null )
244
244
ValidationAssist . SetSuppress ( dialogHost . _popupContentControl , ! dialogHost . IsOpen ) ;
@@ -261,7 +261,12 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
261
261
dialogHost . _session . IsEnded = true ;
262
262
dialogHost . _session = null ;
263
263
dialogHost . _closeCleanUp ( ) ;
264
- dialogHost . Dispatcher . InvokeAsync ( ( ) => dialogHost . _restoreFocusDialogClose . Focus ( ) , DispatcherPriority . Input ) ;
264
+
265
+ // Don't attempt to Invoke if _restoreFocusDialogClose hasn't been assigned yet. Can occur
266
+ // if the MainWindow has started up minimized. Even when Show() has been called, this doesn't
267
+ // seem to have been set.
268
+
269
+ dialogHost . Dispatcher . InvokeAsync ( ( ) => dialogHost . _restoreFocusDialogClose ? . Focus ( ) , DispatcherPriority . Input ) ;
265
270
266
271
return ;
267
272
}
@@ -294,7 +299,7 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
294
299
295
300
public bool IsOpen
296
301
{
297
- get { return ( bool ) GetValue ( IsOpenProperty ) ; }
302
+ get { return ( bool ) GetValue ( IsOpenProperty ) ; }
298
303
set { SetValue ( IsOpenProperty , value ) ; }
299
304
}
300
305
@@ -303,7 +308,7 @@ public bool IsOpen
303
308
304
309
public object DialogContent
305
310
{
306
- get { return ( object ) GetValue ( DialogContentProperty ) ; }
311
+ get { return ( object ) GetValue ( DialogContentProperty ) ; }
307
312
set { SetValue ( DialogContentProperty , value ) ; }
308
313
}
309
314
@@ -312,7 +317,7 @@ public object DialogContent
312
317
313
318
public DataTemplate DialogContentTemplate
314
319
{
315
- get { return ( DataTemplate ) GetValue ( DialogContentTemplateProperty ) ; }
320
+ get { return ( DataTemplate ) GetValue ( DialogContentTemplateProperty ) ; }
316
321
set { SetValue ( DialogContentTemplateProperty , value ) ; }
317
322
}
318
323
@@ -321,7 +326,7 @@ public DataTemplate DialogContentTemplate
321
326
322
327
public DataTemplateSelector DialogContentTemplateSelector
323
328
{
324
- get { return ( DataTemplateSelector ) GetValue ( DialogContentTemplateSelectorProperty ) ; }
329
+ get { return ( DataTemplateSelector ) GetValue ( DialogContentTemplateSelectorProperty ) ; }
325
330
set { SetValue ( DialogContentTemplateSelectorProperty , value ) ; }
326
331
}
327
332
@@ -330,7 +335,7 @@ public DataTemplateSelector DialogContentTemplateSelector
330
335
331
336
public string DialogContentStringFormat
332
337
{
333
- get { return ( string ) GetValue ( DialogContentStringFormatProperty ) ; }
338
+ get { return ( string ) GetValue ( DialogContentStringFormatProperty ) ; }
334
339
set { SetValue ( DialogContentStringFormatProperty , value ) ; }
335
340
}
336
341
@@ -339,7 +344,7 @@ public string DialogContentStringFormat
339
344
340
345
public Thickness DialogMargin
341
346
{
342
- get { return ( Thickness ) GetValue ( DialogMarginProperty ) ; }
347
+ get { return ( Thickness ) GetValue ( DialogMarginProperty ) ; }
343
348
set { SetValue ( DialogMarginProperty , value ) ; }
344
349
}
345
350
@@ -352,7 +357,7 @@ public Thickness DialogMargin
352
357
/// </summary>
353
358
public DialogHostOpenDialogCommandDataContextSource OpenDialogCommandDataContextSource
354
359
{
355
- get { return ( DialogHostOpenDialogCommandDataContextSource ) GetValue ( OpenDialogCommandDataContextSourceProperty ) ; }
360
+ get { return ( DialogHostOpenDialogCommandDataContextSource ) GetValue ( OpenDialogCommandDataContextSourceProperty ) ; }
356
361
set { SetValue ( OpenDialogCommandDataContextSourceProperty , value ) ; }
357
362
}
358
363
@@ -364,7 +369,7 @@ public DialogHostOpenDialogCommandDataContextSource OpenDialogCommandDataContext
364
369
/// </summary>
365
370
public bool CloseOnClickAway
366
371
{
367
- get { return ( bool ) GetValue ( CloseOnClickAwayProperty ) ; }
372
+ get { return ( bool ) GetValue ( CloseOnClickAwayProperty ) ; }
368
373
set { SetValue ( CloseOnClickAwayProperty , value ) ; }
369
374
}
370
375
@@ -376,7 +381,7 @@ public bool CloseOnClickAway
376
381
/// </summary>
377
382
public object CloseOnClickAwayParameter
378
383
{
379
- get { return ( object ) GetValue ( CloseOnClickAwayParameterProperty ) ; }
384
+ get { return ( object ) GetValue ( CloseOnClickAwayParameterProperty ) ; }
380
385
set { SetValue ( CloseOnClickAwayParameterProperty , value ) ; }
381
386
}
382
387
@@ -395,14 +400,14 @@ private static void SnackbarMessageQueuePropertyChangedCallback(DependencyObject
395
400
if ( ! dialogHost . IsOpen ) return ;
396
401
var snackbarMessageQueue = dependencyPropertyChangedEventArgs . NewValue as SnackbarMessageQueue ;
397
402
dialogHost . _currentSnackbarMessageQueueUnPauseAction = snackbarMessageQueue ? . Pause ( ) ;
398
- }
403
+ }
399
404
400
405
/// <summary>
401
406
/// Allows association of a snackbar, so that notifications can be paused whilst a dialog is being displayed.
402
407
/// </summary>
403
408
public SnackbarMessageQueue SnackbarMessageQueue
404
409
{
405
- get { return ( SnackbarMessageQueue ) GetValue ( SnackbarMessageQueueProperty ) ; }
410
+ get { return ( SnackbarMessageQueue ) GetValue ( SnackbarMessageQueueProperty ) ; }
406
411
set { SetValue ( SnackbarMessageQueueProperty , value ) ; }
407
412
}
408
413
@@ -420,7 +425,7 @@ public override void OnApplyTemplate()
420
425
421
426
VisualStateManager . GoToState ( this , SelectState ( ) , false ) ;
422
427
423
- base . OnApplyTemplate ( ) ;
428
+ base . OnApplyTemplate ( ) ;
424
429
}
425
430
426
431
#region open dialog events/callbacks
@@ -493,7 +498,7 @@ public event DialogClosingEventHandler DialogClosing
493
498
add { AddHandler ( DialogClosingEvent , value ) ; }
494
499
remove { RemoveHandler ( DialogClosingEvent , value ) ; }
495
500
}
496
-
501
+
497
502
/// <summary>
498
503
/// Attached property which can be used on the <see cref="Button"/> which instigated the <see cref="OpenDialogCommand"/> to process the closing event.
499
504
/// </summary>
@@ -507,23 +512,23 @@ public static void SetDialogClosingAttached(DependencyObject element, DialogClos
507
512
508
513
public static DialogClosingEventHandler GetDialogClosingAttached ( DependencyObject element )
509
514
{
510
- return ( DialogClosingEventHandler ) element . GetValue ( DialogClosingAttachedProperty ) ;
511
- }
515
+ return ( DialogClosingEventHandler ) element . GetValue ( DialogClosingAttachedProperty ) ;
516
+ }
512
517
513
518
public static readonly DependencyProperty DialogClosingCallbackProperty = DependencyProperty . Register (
514
- nameof ( DialogClosingCallback ) , typeof ( DialogClosingEventHandler ) , typeof ( DialogHost ) , new PropertyMetadata ( default ( DialogClosingEventHandler ) ) ) ;
519
+ nameof ( DialogClosingCallback ) , typeof ( DialogClosingEventHandler ) , typeof ( DialogHost ) , new PropertyMetadata ( default ( DialogClosingEventHandler ) ) ) ;
515
520
516
521
/// <summary>
517
522
/// Callback fired when the <see cref="DialogClosing"/> event is fired, allowing the event to be processed from a binding/view model.
518
523
/// </summary>
519
524
public DialogClosingEventHandler DialogClosingCallback
520
525
{
521
- get { return ( DialogClosingEventHandler ) GetValue ( DialogClosingCallbackProperty ) ; }
526
+ get { return ( DialogClosingEventHandler ) GetValue ( DialogClosingCallbackProperty ) ; }
522
527
set { SetValue ( DialogClosingCallbackProperty , value ) ; }
523
528
}
524
529
525
530
protected void OnDialogClosing ( DialogClosingEventArgs eventArgs )
526
- {
531
+ {
527
532
RaiseEvent ( eventArgs ) ;
528
533
}
529
534
@@ -541,7 +546,7 @@ internal void Close(object parameter)
541
546
{
542
547
var dialogClosingEventArgs = new DialogClosingEventArgs ( _session , parameter , DialogClosingEvent ) ;
543
548
544
- _session . IsEnded = true ;
549
+ _session . IsEnded = true ;
545
550
546
551
//multiple ways of calling back that the dialog is closing:
547
552
// * routed event
@@ -556,7 +561,7 @@ internal void Close(object parameter)
556
561
if ( ! dialogClosingEventArgs . IsCancelled )
557
562
SetCurrentValue ( IsOpenProperty , false ) ;
558
563
else
559
- _session . IsEnded = false ;
564
+ _session . IsEnded = false ;
560
565
561
566
_closeDialogExecutionParameter = parameter ;
562
567
}
@@ -681,7 +686,7 @@ private static void WatchWindowActivation(DialogHost dialogHost)
681
686
}
682
687
683
688
private void WindowOnDeactivated ( object sender , EventArgs eventArgs )
684
- {
689
+ {
685
690
_restoreFocusWindowReactivation = _popup != null ? FocusManager . GetFocusedElement ( ( Window ) sender ) : null ;
686
691
}
687
692
@@ -690,7 +695,7 @@ private void WindowOnActivated(object sender, EventArgs eventArgs)
690
695
if ( _restoreFocusWindowReactivation != null )
691
696
{
692
697
Dispatcher . BeginInvoke ( new Action ( ( ) =>
693
- {
698
+ {
694
699
Keyboard . Focus ( _restoreFocusWindowReactivation ) ;
695
700
} ) ) ;
696
701
}
0 commit comments