Skip to content

Commit

Permalink
feat: Add support for overriding the MessageDialog style
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Mar 28, 2022
1 parent eb7ee3e commit 95ada29
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ public MessageDialogContentDialog(MessageDialog messageDialog)
DefaultStyleKey = typeof(ContentDialog);
_messageDialog = messageDialog ?? throw new ArgumentNullException(nameof(messageDialog));

// WinUI provides a modern style for ContentDialog, which is not applied automatically - force it
if (Application.Current.Resources.TryGetValue("DefaultContentDialogStyle", out var resource) && resource is Style winUIStyle)
{
Style = winUIStyle;
var styleOverriden = TryApplyStyle(WinRTFeatureConfiguration.MessageDialog.StyleOverride);
if (!styleOverriden)
{
// WinUI provides a modern style for ContentDialog, which is not applied automatically.
// Force apply it if available.
TryApplyStyle("DefaultContentDialogStyle");
}

_commands = _messageDialog.Commands.ToList();
Expand All @@ -49,6 +51,19 @@ public MessageDialogContentDialog(MessageDialog messageDialog)
DefaultButton = (ContentDialogButton)(_messageDialog.DefaultCommandIndex + 1); // ContentDialogButton indexed from 1
}

private bool TryApplyStyle(string resourceKey)
{
if (!string.IsNullOrEmpty(resourceKey) &&
Application.Current.Resources.TryGetValue(resourceKey, out var resource) &&
resource is Style style)
{
Style = style;
return true;
}

return false;
}

public async Task<IUICommand> ShowAsync(CancellationToken ct)
{
var result = await base.ShowAsync().AsTask(ct);
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/UI/Popups/MessageDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public IAsyncOperation<IUICommand> ShowAsync()

private async Task<IUICommand> ShowInnerAsync(CancellationToken ct)
{
#if __IOS__ || __MACOS__ || __ANDROID__
#if __IOS__ || __MACOS__ || __ANDROID__ || __WASM__
if (WinRTFeatureConfiguration.MessageDialog.UseNativeDialog)
{
return await ShowNativeAsync(ct);
Expand Down
7 changes: 7 additions & 0 deletions src/Uno.UWP/WinRTFeatureConfiguration.MessageDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@ public static class MessageDialog
#else
false;
#endif

/// <summary>
/// Allows overriding the style used by the ContentDialog
/// which displays the MessageDialog. Should be set to a name (Key)
/// of a Application-level ContentDialog style resource.
/// </summary>
public static string StyleOverride { get; set; }
}
}

0 comments on commit 95ada29

Please sign in to comment.