Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/Wpf.Ui/Controls/ContentDialog/ContentDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,16 @@ public ContentDialog()
{
SetValue(TemplateButtonCommandProperty, new RelayCommand<ContentDialogButton>(OnButtonClick));

Loaded += static (sender, _) =>
// Avoid registering runtime code that triggers designer behavior or throws exceptions
// at design time (to reduce the possibility of designer crashes/rendering failures).
if (!Wpf.Ui.Designer.DesignerHelper.IsInDesignMode)
{
var self = (ContentDialog)sender;
self.OnLoaded();
};
Loaded += static (sender, _) =>
{
var self = (ContentDialog)sender;
self.OnLoaded();
};
}
}

/// <summary>
Expand All @@ -492,11 +497,16 @@ public ContentDialog(ContentPresenter? dialogHost)

SetValue(TemplateButtonCommandProperty, new RelayCommand<ContentDialogButton>(OnButtonClick));

Loaded += static (sender, _) =>
// Avoid registering runtime code that triggers designer behavior or throws exceptions
// at design time (to reduce the possibility of designer crashes/rendering failures).
if (!Wpf.Ui.Designer.DesignerHelper.IsInDesignMode)
{
var self = (ContentDialog)sender;
self.OnLoaded();
};
Loaded += static (sender, _) =>
{
var self = (ContentDialog)sender;
self.OnLoaded();
};
}
}

/// <summary>
Expand Down Expand Up @@ -601,6 +611,12 @@ protected virtual void OnButtonClick(ContentDialogButton button)

protected override Size MeasureOverride(Size availableSize)
{
// Avoid throwing exceptions when visual child elements cannot be obtained (designer or template not applied).
if (VisualChildrenCount == 0)
{
return base.MeasureOverride(availableSize);
}

var rootElement = (UIElement)GetVisualChild(0)!;

rootElement.Measure(availableSize);
Expand All @@ -622,6 +638,7 @@ protected override Size MeasureOverride(Size availableSize)
/// </summary>
protected virtual void OnLoaded()
{
// Focus is only needed at runtime.
_ = Focus();

RaiseEvent(new RoutedEventArgs(OpenedEvent));
Expand Down
Loading