Skip to content

Commit e38fa72

Browse files
authored
Merge pull request #549 from AmitBhatnagar24/master
Null Check on _restoreFocusDialogClose before Invoke to fix issue #548
2 parents ec69ebc + 76057d0 commit e38fa72

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ public class DialogHost : ContentControl
6767
private Grid _contentCoverGrid;
6868
private DialogSession _session;
6969
private DialogOpenedEventHandler _attachedDialogOpenedEventHandler;
70-
private DialogClosingEventHandler _attachedDialogClosingEventHandler;
70+
private DialogClosingEventHandler _attachedDialogClosingEventHandler;
7171
private object _closeDialogExecutionParameter;
72-
private IInputElement _restoreFocusDialogClose;
72+
private IInputElement _restoreFocusDialogClose;
7373
private IInputElement _restoreFocusWindowReactivation;
7474
private Action _currentSnackbarMessageQueueUnPauseAction = null;
7575
private Action _closeCleanUp = () => { };
7676

7777
static DialogHost()
7878
{
79-
DefaultStyleKeyProperty.OverrideMetadata(typeof(DialogHost), new FrameworkPropertyMetadata(typeof(DialogHost)));
79+
DefaultStyleKeyProperty.OverrideMetadata(typeof(DialogHost), new FrameworkPropertyMetadata(typeof(DialogHost)));
8080
}
8181

8282
#region .Show overloads
@@ -158,7 +158,7 @@ public static Task<object> Show(object content, object dialogIdentifier, DialogO
158158
public static Task<object> Show(object content, object dialogIdentifier, DialogClosingEventHandler closingEventHandler)
159159
{
160160
return Show(content, dialogIdentifier, null, closingEventHandler);
161-
}
161+
}
162162

163163
/// <summary>
164164
/// 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()
218218
Unloaded += OnUnloaded;
219219

220220
CommandBindings.Add(new CommandBinding(CloseDialogCommand, CloseDialogHandler, CloseDialogCanExecute));
221-
CommandBindings.Add(new CommandBinding(OpenDialogCommand, OpenDialogHandler));
221+
CommandBindings.Add(new CommandBinding(OpenDialogCommand, OpenDialogHandler));
222222
}
223223

224224
public static readonly DependencyProperty IdentifierProperty = DependencyProperty.Register(
@@ -238,7 +238,7 @@ public object Identifier
238238

239239
private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
240240
{
241-
var dialogHost = (DialogHost)dependencyObject;
241+
var dialogHost = (DialogHost)dependencyObject;
242242

243243
if (dialogHost._popupContentControl != null)
244244
ValidationAssist.SetSuppress(dialogHost._popupContentControl, !dialogHost.IsOpen);
@@ -261,7 +261,12 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
261261
dialogHost._session.IsEnded = true;
262262
dialogHost._session = null;
263263
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);
265270

266271
return;
267272
}
@@ -294,7 +299,7 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
294299

295300
public bool IsOpen
296301
{
297-
get { return (bool) GetValue(IsOpenProperty); }
302+
get { return (bool)GetValue(IsOpenProperty); }
298303
set { SetValue(IsOpenProperty, value); }
299304
}
300305

@@ -303,7 +308,7 @@ public bool IsOpen
303308

304309
public object DialogContent
305310
{
306-
get { return (object) GetValue(DialogContentProperty); }
311+
get { return (object)GetValue(DialogContentProperty); }
307312
set { SetValue(DialogContentProperty, value); }
308313
}
309314

@@ -312,7 +317,7 @@ public object DialogContent
312317

313318
public DataTemplate DialogContentTemplate
314319
{
315-
get { return (DataTemplate) GetValue(DialogContentTemplateProperty); }
320+
get { return (DataTemplate)GetValue(DialogContentTemplateProperty); }
316321
set { SetValue(DialogContentTemplateProperty, value); }
317322
}
318323

@@ -321,7 +326,7 @@ public DataTemplate DialogContentTemplate
321326

322327
public DataTemplateSelector DialogContentTemplateSelector
323328
{
324-
get { return (DataTemplateSelector) GetValue(DialogContentTemplateSelectorProperty); }
329+
get { return (DataTemplateSelector)GetValue(DialogContentTemplateSelectorProperty); }
325330
set { SetValue(DialogContentTemplateSelectorProperty, value); }
326331
}
327332

@@ -330,7 +335,7 @@ public DataTemplateSelector DialogContentTemplateSelector
330335

331336
public string DialogContentStringFormat
332337
{
333-
get { return (string) GetValue(DialogContentStringFormatProperty); }
338+
get { return (string)GetValue(DialogContentStringFormatProperty); }
334339
set { SetValue(DialogContentStringFormatProperty, value); }
335340
}
336341

@@ -339,7 +344,7 @@ public string DialogContentStringFormat
339344

340345
public Thickness DialogMargin
341346
{
342-
get { return (Thickness) GetValue(DialogMarginProperty); }
347+
get { return (Thickness)GetValue(DialogMarginProperty); }
343348
set { SetValue(DialogMarginProperty, value); }
344349
}
345350

@@ -352,7 +357,7 @@ public Thickness DialogMargin
352357
/// </summary>
353358
public DialogHostOpenDialogCommandDataContextSource OpenDialogCommandDataContextSource
354359
{
355-
get { return (DialogHostOpenDialogCommandDataContextSource) GetValue(OpenDialogCommandDataContextSourceProperty); }
360+
get { return (DialogHostOpenDialogCommandDataContextSource)GetValue(OpenDialogCommandDataContextSourceProperty); }
356361
set { SetValue(OpenDialogCommandDataContextSourceProperty, value); }
357362
}
358363

@@ -364,7 +369,7 @@ public DialogHostOpenDialogCommandDataContextSource OpenDialogCommandDataContext
364369
/// </summary>
365370
public bool CloseOnClickAway
366371
{
367-
get { return (bool) GetValue(CloseOnClickAwayProperty); }
372+
get { return (bool)GetValue(CloseOnClickAwayProperty); }
368373
set { SetValue(CloseOnClickAwayProperty, value); }
369374
}
370375

@@ -376,7 +381,7 @@ public bool CloseOnClickAway
376381
/// </summary>
377382
public object CloseOnClickAwayParameter
378383
{
379-
get { return (object) GetValue(CloseOnClickAwayParameterProperty); }
384+
get { return (object)GetValue(CloseOnClickAwayParameterProperty); }
380385
set { SetValue(CloseOnClickAwayParameterProperty, value); }
381386
}
382387

@@ -395,14 +400,14 @@ private static void SnackbarMessageQueuePropertyChangedCallback(DependencyObject
395400
if (!dialogHost.IsOpen) return;
396401
var snackbarMessageQueue = dependencyPropertyChangedEventArgs.NewValue as SnackbarMessageQueue;
397402
dialogHost._currentSnackbarMessageQueueUnPauseAction = snackbarMessageQueue?.Pause();
398-
}
403+
}
399404

400405
/// <summary>
401406
/// Allows association of a snackbar, so that notifications can be paused whilst a dialog is being displayed.
402407
/// </summary>
403408
public SnackbarMessageQueue SnackbarMessageQueue
404409
{
405-
get { return (SnackbarMessageQueue) GetValue(SnackbarMessageQueueProperty); }
410+
get { return (SnackbarMessageQueue)GetValue(SnackbarMessageQueueProperty); }
406411
set { SetValue(SnackbarMessageQueueProperty, value); }
407412
}
408413

@@ -420,7 +425,7 @@ public override void OnApplyTemplate()
420425

421426
VisualStateManager.GoToState(this, SelectState(), false);
422427

423-
base.OnApplyTemplate();
428+
base.OnApplyTemplate();
424429
}
425430

426431
#region open dialog events/callbacks
@@ -493,7 +498,7 @@ public event DialogClosingEventHandler DialogClosing
493498
add { AddHandler(DialogClosingEvent, value); }
494499
remove { RemoveHandler(DialogClosingEvent, value); }
495500
}
496-
501+
497502
/// <summary>
498503
/// Attached property which can be used on the <see cref="Button"/> which instigated the <see cref="OpenDialogCommand"/> to process the closing event.
499504
/// </summary>
@@ -507,23 +512,23 @@ public static void SetDialogClosingAttached(DependencyObject element, DialogClos
507512

508513
public static DialogClosingEventHandler GetDialogClosingAttached(DependencyObject element)
509514
{
510-
return (DialogClosingEventHandler) element.GetValue(DialogClosingAttachedProperty);
511-
}
515+
return (DialogClosingEventHandler)element.GetValue(DialogClosingAttachedProperty);
516+
}
512517

513518
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)));
515520

516521
/// <summary>
517522
/// Callback fired when the <see cref="DialogClosing"/> event is fired, allowing the event to be processed from a binding/view model.
518523
/// </summary>
519524
public DialogClosingEventHandler DialogClosingCallback
520525
{
521-
get { return (DialogClosingEventHandler) GetValue(DialogClosingCallbackProperty); }
526+
get { return (DialogClosingEventHandler)GetValue(DialogClosingCallbackProperty); }
522527
set { SetValue(DialogClosingCallbackProperty, value); }
523528
}
524529

525530
protected void OnDialogClosing(DialogClosingEventArgs eventArgs)
526-
{
531+
{
527532
RaiseEvent(eventArgs);
528533
}
529534

@@ -541,7 +546,7 @@ internal void Close(object parameter)
541546
{
542547
var dialogClosingEventArgs = new DialogClosingEventArgs(_session, parameter, DialogClosingEvent);
543548

544-
_session.IsEnded = true;
549+
_session.IsEnded = true;
545550

546551
//multiple ways of calling back that the dialog is closing:
547552
// * routed event
@@ -556,7 +561,7 @@ internal void Close(object parameter)
556561
if (!dialogClosingEventArgs.IsCancelled)
557562
SetCurrentValue(IsOpenProperty, false);
558563
else
559-
_session.IsEnded = false;
564+
_session.IsEnded = false;
560565

561566
_closeDialogExecutionParameter = parameter;
562567
}
@@ -681,7 +686,7 @@ private static void WatchWindowActivation(DialogHost dialogHost)
681686
}
682687

683688
private void WindowOnDeactivated(object sender, EventArgs eventArgs)
684-
{
689+
{
685690
_restoreFocusWindowReactivation = _popup != null ? FocusManager.GetFocusedElement((Window)sender) : null;
686691
}
687692

@@ -690,7 +695,7 @@ private void WindowOnActivated(object sender, EventArgs eventArgs)
690695
if (_restoreFocusWindowReactivation != null)
691696
{
692697
Dispatcher.BeginInvoke(new Action(() =>
693-
{
698+
{
694699
Keyboard.Focus(_restoreFocusWindowReactivation);
695700
}));
696701
}

0 commit comments

Comments
 (0)