ThemeResource 颜色不遵循应用程序的主题 UWP

问题描述 投票:0回答:1

我有一个以编程方式为我的 UWP 应用程序创建的浮出控件,我将 WinUI 库中的

SystemFillColorCautionBackgroundBrush
背景颜色应用到它:

//create a flyout
var flyout = new Flyout();

//create a textblock
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);

var flyoutPresenterStyle = new Style(typeof(FlyoutPresenter));

flyoutPresenterStyle.Setters.Add(
    new Setter(
        FlyoutPresenter.BackgroundProperty,
        (Windows.UI.Xaml.Media.Brush)
            Application.Current.Resources["SystemFillColorCautionBackgroundBrush"]
    )
);

//make the flyout wrap the text vertically
flyoutPresenterStyle.Setters.Add(
    new Setter(ScrollViewer.HorizontalScrollModeProperty, ScrollMode.Disabled)
);
flyoutPresenterStyle.Setters.Add(
    new Setter(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled)
);

//make the flyoutPresenterStyle based on the default one
flyoutPresenterStyle.BasedOn = (Style)Application.Current.Resources["DefaultFlyoutPresenterStyle"];

flyout.FlyoutPresenterStyle = flyoutPresenterStyle;

//create a button
var button = new Button();
button.Content = "Leggi e apri";

问题是我选择的背景颜色的主题不会根据我的应用请求的主题而改变:

如何设置弹出框背景颜色的主题?

更新 1: 此外,我观察到在 Windows 11 版本 22H2 上不存在此问题。这只发生在 Windows 10 上。

c# uwp winui
1个回答
0
投票

推荐在xaml中使用ThemeResource,当主题改变时可以重新计算,代码如下

<Button Content="Empty cart">
    <Button.Flyout>
        <Flyout>
            <Flyout.FlyoutPresenterStyle>
                <Style TargetType="FlyoutPresenter">
                    <Setter Property="Background" Value="{ThemeResource SystemFillColorCautionBackgroundBrush}"/>
                </Style>
            </Flyout.FlyoutPresenterStyle>
            <TextBlock TextWrapping="Wrap" Text="This is some text in a flyout."/>
        </Flyout>
    </Button.Flyout>
</Button>

编辑 2023/5/3

如果你想从代码中访问它,你可以创建一个新的自定义 Flyout 样式,并从代码中使用它。为了进行测试,我在 App.xaml.cs 中定义了样式。可以参考下面的代码

App.xaml.cs

<Application.Resources>
    <controls:XamlControlsResources>
        <controls:XamlControlsResources.MergedDictionaries>
            <!-- Other app resources here -->
            <ResourceDictionary>
                <Style x:Name="NewflyoutStyle" TargetType="FlyoutPresenter" BasedOn="{StaticResource DefaultFlyoutPresenterStyle}">
                    <Setter Property="Background" Value="{ThemeResource SystemFillColorCautionBackgroundBrush}" />
                </Style>
            </ResourceDictionary>
        </controls:XamlControlsResources.MergedDictionaries>
    </controls:XamlControlsResources>
</Application.Resources>

Page.xaml.cs

var flyoutPresenterStyle = new Style(typeof(FlyoutPresenter));

//flyoutPresenterStyle.Setters.Add(
//    new Setter(
//        FlyoutPresenter.BackgroundProperty,
//        (Windows.UI.Xaml.Media.Brush)
//            Application.Current.Resources["SystemFillColorCautionBackgroundBrush"]
//    )
//);

//make the flyout wrap the text vertically
flyoutPresenterStyle.Setters.Add(
    new Setter(ScrollViewer.HorizontalScrollModeProperty, ScrollMode.Disabled)
);
flyoutPresenterStyle.Setters.Add(
    new Setter(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled)
);

//make the flyoutPresenterStyle based on the custom one
flyoutPresenterStyle.BasedOn = (Style)Application.Current.Resources["NewflyoutStyle"];

flyout.FlyoutPresenterStyle = flyoutPresenterStyle;
© www.soinside.com 2019 - 2024. All rights reserved.