@@ -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,8 +261,14 @@ 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 ) ;
265
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
+ if ( dialogHost . _restoreFocusDialogClose != null )
269
+ {
270
+ dialogHost . Dispatcher . InvokeAsync ( ( ) => dialogHost . _restoreFocusDialogClose . Focus ( ) , DispatcherPriority . Input ) ;
271
+ }
266
272
return ;
267
273
}
268
274
@@ -294,7 +300,7 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
294
300
295
301
public bool IsOpen
296
302
{
297
- get { return ( bool ) GetValue ( IsOpenProperty ) ; }
303
+ get { return ( bool ) GetValue ( IsOpenProperty ) ; }
298
304
set { SetValue ( IsOpenProperty , value ) ; }
299
305
}
300
306
@@ -303,7 +309,7 @@ public bool IsOpen
303
309
304
310
public object DialogContent
305
311
{
306
- get { return ( object ) GetValue ( DialogContentProperty ) ; }
312
+ get { return ( object ) GetValue ( DialogContentProperty ) ; }
307
313
set { SetValue ( DialogContentProperty , value ) ; }
308
314
}
309
315
@@ -312,7 +318,7 @@ public object DialogContent
312
318
313
319
public DataTemplate DialogContentTemplate
314
320
{
315
- get { return ( DataTemplate ) GetValue ( DialogContentTemplateProperty ) ; }
321
+ get { return ( DataTemplate ) GetValue ( DialogContentTemplateProperty ) ; }
316
322
set { SetValue ( DialogContentTemplateProperty , value ) ; }
317
323
}
318
324
@@ -321,7 +327,7 @@ public DataTemplate DialogContentTemplate
321
327
322
328
public DataTemplateSelector DialogContentTemplateSelector
323
329
{
324
- get { return ( DataTemplateSelector ) GetValue ( DialogContentTemplateSelectorProperty ) ; }
330
+ get { return ( DataTemplateSelector ) GetValue ( DialogContentTemplateSelectorProperty ) ; }
325
331
set { SetValue ( DialogContentTemplateSelectorProperty , value ) ; }
326
332
}
327
333
@@ -330,7 +336,7 @@ public DataTemplateSelector DialogContentTemplateSelector
330
336
331
337
public string DialogContentStringFormat
332
338
{
333
- get { return ( string ) GetValue ( DialogContentStringFormatProperty ) ; }
339
+ get { return ( string ) GetValue ( DialogContentStringFormatProperty ) ; }
334
340
set { SetValue ( DialogContentStringFormatProperty , value ) ; }
335
341
}
336
342
@@ -339,7 +345,7 @@ public string DialogContentStringFormat
339
345
340
346
public Thickness DialogMargin
341
347
{
342
- get { return ( Thickness ) GetValue ( DialogMarginProperty ) ; }
348
+ get { return ( Thickness ) GetValue ( DialogMarginProperty ) ; }
343
349
set { SetValue ( DialogMarginProperty , value ) ; }
344
350
}
345
351
@@ -352,7 +358,7 @@ public Thickness DialogMargin
352
358
/// </summary>
353
359
public DialogHostOpenDialogCommandDataContextSource OpenDialogCommandDataContextSource
354
360
{
355
- get { return ( DialogHostOpenDialogCommandDataContextSource ) GetValue ( OpenDialogCommandDataContextSourceProperty ) ; }
361
+ get { return ( DialogHostOpenDialogCommandDataContextSource ) GetValue ( OpenDialogCommandDataContextSourceProperty ) ; }
356
362
set { SetValue ( OpenDialogCommandDataContextSourceProperty , value ) ; }
357
363
}
358
364
@@ -364,7 +370,7 @@ public DialogHostOpenDialogCommandDataContextSource OpenDialogCommandDataContext
364
370
/// </summary>
365
371
public bool CloseOnClickAway
366
372
{
367
- get { return ( bool ) GetValue ( CloseOnClickAwayProperty ) ; }
373
+ get { return ( bool ) GetValue ( CloseOnClickAwayProperty ) ; }
368
374
set { SetValue ( CloseOnClickAwayProperty , value ) ; }
369
375
}
370
376
@@ -376,7 +382,7 @@ public bool CloseOnClickAway
376
382
/// </summary>
377
383
public object CloseOnClickAwayParameter
378
384
{
379
- get { return ( object ) GetValue ( CloseOnClickAwayParameterProperty ) ; }
385
+ get { return ( object ) GetValue ( CloseOnClickAwayParameterProperty ) ; }
380
386
set { SetValue ( CloseOnClickAwayParameterProperty , value ) ; }
381
387
}
382
388
@@ -395,14 +401,14 @@ private static void SnackbarMessageQueuePropertyChangedCallback(DependencyObject
395
401
if ( ! dialogHost . IsOpen ) return ;
396
402
var snackbarMessageQueue = dependencyPropertyChangedEventArgs . NewValue as SnackbarMessageQueue ;
397
403
dialogHost . _currentSnackbarMessageQueueUnPauseAction = snackbarMessageQueue ? . Pause ( ) ;
398
- }
404
+ }
399
405
400
406
/// <summary>
401
407
/// Allows association of a snackbar, so that notifications can be paused whilst a dialog is being displayed.
402
408
/// </summary>
403
409
public SnackbarMessageQueue SnackbarMessageQueue
404
410
{
405
- get { return ( SnackbarMessageQueue ) GetValue ( SnackbarMessageQueueProperty ) ; }
411
+ get { return ( SnackbarMessageQueue ) GetValue ( SnackbarMessageQueueProperty ) ; }
406
412
set { SetValue ( SnackbarMessageQueueProperty , value ) ; }
407
413
}
408
414
@@ -420,7 +426,7 @@ public override void OnApplyTemplate()
420
426
421
427
VisualStateManager . GoToState ( this , SelectState ( ) , false ) ;
422
428
423
- base . OnApplyTemplate ( ) ;
429
+ base . OnApplyTemplate ( ) ;
424
430
}
425
431
426
432
#region open dialog events/callbacks
@@ -493,7 +499,7 @@ public event DialogClosingEventHandler DialogClosing
493
499
add { AddHandler ( DialogClosingEvent , value ) ; }
494
500
remove { RemoveHandler ( DialogClosingEvent , value ) ; }
495
501
}
496
-
502
+
497
503
/// <summary>
498
504
/// Attached property which can be used on the <see cref="Button"/> which instigated the <see cref="OpenDialogCommand"/> to process the closing event.
499
505
/// </summary>
@@ -507,23 +513,23 @@ public static void SetDialogClosingAttached(DependencyObject element, DialogClos
507
513
508
514
public static DialogClosingEventHandler GetDialogClosingAttached ( DependencyObject element )
509
515
{
510
- return ( DialogClosingEventHandler ) element . GetValue ( DialogClosingAttachedProperty ) ;
511
- }
516
+ return ( DialogClosingEventHandler ) element . GetValue ( DialogClosingAttachedProperty ) ;
517
+ }
512
518
513
519
public static readonly DependencyProperty DialogClosingCallbackProperty = DependencyProperty . Register (
514
- nameof ( DialogClosingCallback ) , typeof ( DialogClosingEventHandler ) , typeof ( DialogHost ) , new PropertyMetadata ( default ( DialogClosingEventHandler ) ) ) ;
520
+ nameof ( DialogClosingCallback ) , typeof ( DialogClosingEventHandler ) , typeof ( DialogHost ) , new PropertyMetadata ( default ( DialogClosingEventHandler ) ) ) ;
515
521
516
522
/// <summary>
517
523
/// Callback fired when the <see cref="DialogClosing"/> event is fired, allowing the event to be processed from a binding/view model.
518
524
/// </summary>
519
525
public DialogClosingEventHandler DialogClosingCallback
520
526
{
521
- get { return ( DialogClosingEventHandler ) GetValue ( DialogClosingCallbackProperty ) ; }
527
+ get { return ( DialogClosingEventHandler ) GetValue ( DialogClosingCallbackProperty ) ; }
522
528
set { SetValue ( DialogClosingCallbackProperty , value ) ; }
523
529
}
524
530
525
531
protected void OnDialogClosing ( DialogClosingEventArgs eventArgs )
526
- {
532
+ {
527
533
RaiseEvent ( eventArgs ) ;
528
534
}
529
535
@@ -541,7 +547,7 @@ internal void Close(object parameter)
541
547
{
542
548
var dialogClosingEventArgs = new DialogClosingEventArgs ( _session , parameter , DialogClosingEvent ) ;
543
549
544
- _session . IsEnded = true ;
550
+ _session . IsEnded = true ;
545
551
546
552
//multiple ways of calling back that the dialog is closing:
547
553
// * routed event
@@ -556,7 +562,7 @@ internal void Close(object parameter)
556
562
if ( ! dialogClosingEventArgs . IsCancelled )
557
563
SetCurrentValue ( IsOpenProperty , false ) ;
558
564
else
559
- _session . IsEnded = false ;
565
+ _session . IsEnded = false ;
560
566
561
567
_closeDialogExecutionParameter = parameter ;
562
568
}
@@ -681,7 +687,7 @@ private static void WatchWindowActivation(DialogHost dialogHost)
681
687
}
682
688
683
689
private void WindowOnDeactivated ( object sender , EventArgs eventArgs )
684
- {
690
+ {
685
691
_restoreFocusWindowReactivation = _popup != null ? FocusManager . GetFocusedElement ( ( Window ) sender ) : null ;
686
692
}
687
693
@@ -690,7 +696,7 @@ private void WindowOnActivated(object sender, EventArgs eventArgs)
690
696
if ( _restoreFocusWindowReactivation != null )
691
697
{
692
698
Dispatcher . BeginInvoke ( new Action ( ( ) =>
693
- {
699
+ {
694
700
Keyboard . Focus ( _restoreFocusWindowReactivation ) ;
695
701
} ) ) ;
696
702
}
0 commit comments