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

UWP中Flyout的ThemeResource背景色不跟随应用主题求助

UWP Flyout背景色不随主题切换的解决方法(仅Windows 10存在)

我在UWP应用中通过代码创建Flyout时,为其设置了WinUI库的SystemFillColorCautionBackgroundBrush作为背景色,但发现背景色不会随应用主题切换而变化。该问题仅出现在Windows 10系统中,Windows 11 22H2版本无此异常。

原实现代码

// 创建Flyout
var flyout = new Flyout();

// 创建文本块
var textBlock = new TextBlock();
textBlock.Text =
    "Prima di salvare l'allegato è necessario contrassegnare la comunicazione come letta sul server. Confermi?";
textBlock.TextWrapping = TextWrapping.WrapWholeWords;
textBlock.Margin = new Thickness(0, 0, 0, 12);

// 创建FlyoutPresenter样式
var flyoutPresenterStyle = new Style(typeof(FlyoutPresenter));

// 设置背景色(静态获取资源,导致主题切换不更新)
flyoutPresenterStyle.Setters.Add(
    new Setter(
        FlyoutPresenter.BackgroundProperty,
        (Windows.UI.Xaml.Media.Brush)
            Application.Current.Resources["SystemFillColorCautionBackgroundBrush"]
    )
);

// 禁用水平滚动,让文本自动换行
flyoutPresenterStyle.Setters.Add(
    new Setter(ScrollViewer.HorizontalScrollModeProperty, ScrollMode.Disabled)
);
flyoutPresenterStyle.Setters.Add(
    new Setter(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled)
);

// 基于默认样式扩展
flyoutPresenterStyle.BasedOn = (Style)Application.Current.Resources["DefaultFlyoutPresenterStyle"];

flyout.FlyoutPresenterStyle = flyoutPresenterStyle;

// 创建按钮
var button = new Button();
button.Content = "Leggi e apri";

问题原因

Windows 10中,直接通过Application.Current.Resources[key]获取资源时,只会拿到当前主题下的静态实例,不会监听后续的主题切换事件,因此背景色不会自动更新。而Windows 11的WinUI资源系统对此做了优化,所以没有这个问题。

解决方法

方法1:使用动态资源引用(推荐)

在代码中改用DynamicResourceExtension来绑定背景色,这样资源会自动跟随主题变化更新:

修改设置背景色的代码段:

flyoutPresenterStyle.Setters.Add(
    new Setter(
        FlyoutPresenter.BackgroundProperty,
        new Windows.UI.Xaml.DynamicResourceExtension("SystemFillColorCautionBackgroundBrush")
    )
);

DynamicResource会持续监听资源字典的变化,当应用主题切换时,会自动重新获取对应主题下的SystemFillColorCautionBackgroundBrush实例,实现背景色的动态更新。

方法2:手动监听主题变化事件

如果方法1不生效,可以手动监听应用主题变更事件,在主题切换时重新设置背景色:

// 创建Flyout后添加主题监听
Application.Current.RequestedThemeChanged += (sender, args) =>
{
    if (flyout?.FlyoutPresenterStyle == null) return;
    
    // 移除旧的背景色设置
    var existingSetter = flyout.FlyoutPresenterStyle.Setters
        .OfType<Setter>()
        .FirstOrDefault(s => s.Property == FlyoutPresenter.BackgroundProperty);
    if (existingSetter != null)
    {
        flyout.FlyoutPresenterStyle.Setters.Remove(existingSetter);
    }
    
    // 获取当前主题下的新背景色并设置
    var newBrush = (Windows.UI.Xaml.Media.Brush)Application.Current.Resources["SystemFillColorCautionBackgroundBrush"];
    flyout.FlyoutPresenterStyle.Setters.Add(new Setter(FlyoutPresenter.BackgroundProperty, newBrush));
};

注意事项

  • 确保项目已正确引用WinUI 2.x库(WinUI 3针对Win10的适配需额外注意);
  • 确认SystemFillColorCautionBackgroundBrush资源存在于当前WinUI版本的资源字典中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 03:15:40