You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin中使用Acr.UserDialog时,UWP平台如何修改提示框标题字体大小?

How to Modify Alert Title Font Size for UWP in Acr.UserDialogs

Great question! On UWP, the AlertAsync from Acr.UserDialogs relies on the native UWP ContentDialog component under the hood. Here are two reliable ways to tweak the title's font size to fit your needs:

Option 1: Override the Global ContentDialog Style (Quick Global Change)

UWP lets you override default control styles at the app level. This approach will affect all ContentDialog instances in your UWP app (including those generated by Acr.UserDialogs):

  1. Open your UWP project's App.xaml file, and add a custom ContentDialog style. We'll keep most of the native template intact, only adjusting the title's font size:
<Application.Resources>
    <Style TargetType="ContentDialog">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentDialog">
                    <Border x:Name="Container">
                        <Grid x:Name="LayoutRoot">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <!-- Customize the title's FontSize here -->
                            <ContentControl x:Name="Title" 
                                            Grid.Row="0"
                                            FontSize="24" <!-- Set your desired size here -->
                                            Margin="{ThemeResource ContentDialogTitleMargin}"
                                            Content="{TemplateBinding Title}"
                                            ContentTemplate="{TemplateBinding TitleTemplate}"
                                            FontWeight="Normal"
                                            Foreground="{ThemeResource ContentDialogTitleForeground}"
                                            HorizontalAlignment="Left"
                                            VerticalAlignment="Top"
                                            IsTabStop="False" />
                            <!-- Keep the rest of the native template as-is -->
                            <ContentPresenter x:Name="Content"
                                              Grid.Row="1"
                                              ContentTemplate="{TemplateBinding ContentTemplate}"
                                              Content="{TemplateBinding Content}"
                                              Foreground="{ThemeResource ContentDialogContentForeground}"
                                              Margin="{ThemeResource ContentDialogContentMargin}"
                                              HorizontalAlignment="Left"
                                              VerticalAlignment="Top"
                                              IsTabStop="False" />
                            <Grid x:Name="CommandSpace"
                                  Grid.Row="2"
                                  HorizontalAlignment="Stretch"
                                  VerticalAlignment="Bottom">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <Button x:Name="PrimaryButton"
                                        Content="{TemplateBinding PrimaryButtonText}"
                                        Command="{TemplateBinding PrimaryButtonCommand}"
                                        CommandParameter="{TemplateBinding PrimaryButtonCommandParameter}"
                                        Style="{StaticResource AccentButtonStyle}"
                                        Margin="{ThemeResource ContentDialogButtonMargin}"
                                        IsDefault="True"
                                        IsTabStop="True" />
                                <Button x:Name="SecondaryButton"
                                        Grid.Column="1"
                                        Content="{TemplateBinding SecondaryButtonText}"
                                        Command="{TemplateBinding SecondaryButtonCommand}"
                                        CommandParameter="{TemplateBinding SecondaryButtonCommandParameter}"
                                        Style="{StaticResource TextBlockButtonStyle}"
                                        Margin="{ThemeResource ContentDialogButtonMargin}"
                                        IsTabStop="True" />
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

Pro tip: If you want the exact native template to modify (to avoid missing any default behavior), you can extract it from Visual Studio's XAML designer: right-click a ContentDialog -> Edit Template -> Edit a Copy.

Option 2: Custom Alert Dialog Implementation (Targeted Change)

If you don't want to affect all ContentDialogs in your app, you can replace Acr.UserDialogs' default alert with a custom one tailored specifically to your font size needs:

  1. Create a custom dialog class in your UWP project that implements IAlertDialog:
using Acr.UserDialogs;
using Windows.UI.Xaml.Controls;

public class CustomFontAlertDialog : ContentDialog, IAlertDialog
{
    public CustomFontAlertDialog(AlertConfig config)
    {
        Title = config.Title;
        Content = config.Message;
        PrimaryButtonText = config.OkText;

        // Wait for the template to load before modifying the title control
        this.Loaded += (s, e) =>
        {
            if (this.GetTemplateChild("Title") is ContentControl titleControl)
            {
                titleControl.FontSize = 24; // Set your custom font size here
            }
        };

        // Hook up the close event to match Acr's expected behavior
        PrimaryButtonClick += (s, e) => OnClosed?.Invoke();
    }

    public void Show() => _ = this.ShowAsync();
    public void Hide() => this.Hide();
    public event Action OnClosed;
}
  1. Register this custom dialog with Acr.UserDialogs when your UWP app launches (in App.xaml.cs):
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    // Initialize Acr.UserDialogs first
    UserDialogs.Init();

    // Replace the default alert factory with our custom implementation
    UserDialogs.Instance.AlertDialogFactory = config => new CustomFontAlertDialog(config);

    // Rest of your app initialization code...
}

Quick Notes

  • Option 1 is the fastest if you want all dialogs in your app to share the same title font size; Option 2 is better for targeting only Acr.UserDialogs alerts.
  • If the AlertDialogFactory property isn't available, double-check your Acr.UserDialogs version—some older releases might require a custom renderer instead.

内容的提问来源于stack exchange,提问作者sangita malviya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 17:14:08