如何以编程方式构造Windows :: UI :: Xaml :: Style类

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

我正在尝试以编程方式为所有按钮构造一个winrt::Windows::UI::Xaml::Style对象。但是,某些样式Setter无法正常工作。

总而言之,SetterBorderThicknessBorderBrushForeground属性的Margin不起作用。另一方面,SetterFontSize属性的Background正常工作。我该如何解决此问题。

Style对象的构造类似于此代码。

void MainPage::SetStylePropertySetters()
{
    coloredButtonStyle.Setters().Append(Setter{ // doesn't work
        Control::BorderThicknessProperty(),
        box_value(ThicknessHelper::FromUniformLength(10.5)) });

    coloredButtonStyle.Setters().Append(Setter{ // doesn't work
        Control::BorderBrushProperty(),
        box_value(Colors::Black()) });

    coloredButtonStyle.Setters().Append(Setter{ // works right
        Control::FontSizeProperty(),
        box_value(50) });

    coloredButtonStyle.Setters().Append(Setter{ // doesn't work
        Control::ForegroundProperty(),
        box_value(Colors::White()) });

    coloredButtonStyle.Setters().Append(Setter{ // doesn't work
        FrameworkElement::MarginProperty(),
        box_value(ThicknessHelper::FromLengths(10, 10, 0, 0)) });

    coloredButtonStyle.Setters().Append(Setter{ // works right
        Control::BackgroundProperty(),
        GenerateGradient() });
}

LinearGradientBrush MainPage::GenerateGradient()
{
    GradientStopCollection gradientStopCollection{};

    GradientStop gs1;
    gs1.Color(Colors::Yellow());
    gs1.Offset(0);
    gradientStopCollection.Append(gs1);

    GradientStop gs2;
    gs2.Color(Colors::Orange());
    gs2.Offset(0.5);
    gradientStopCollection.Append(gs2);

    GradientStop gs3;
    gs3.Color(Colors::Red());
    gs3.Offset(1.0);
    gradientStopCollection.Append(gs3);

    return LinearGradientBrush(gradientStopCollection, 0.0);
}

然后将样式应用于MainPage的构造函数中的按钮。

MainPage::MainPage() :
    coloredButtonStyle{ xaml_typename<Button>() }
{
    InitializeComponent();

    // Apply a style to a button named DefaultButton
    SetStylePropertySetters();
    DefaultButton().Style(coloredButtonStyle);
}

主页上有一个名为DefaultButton的按钮。

<Page
    x:Class="HeadedAppDesign.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HeadedAppDesign"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel>
        <Button x:Name="DefaultButton" Content="Button"/>
    </StackPanel>
</Page>

这将产生以下结果:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS8xenRWUy5wbmcifQ==” alt =“渲染的输出”>

winrt-xaml c++-winrt
1个回答
0
投票

对于Thickness类型的属性,将字符串对象传递为相同的字符串XAML标记中的格式。对于Brush类型的属性,传递Brush类的派生类对象之一。

void MainPage::SetStylePropertySetters()
{
    coloredButtonStyle.Setters().Append(Setter{
        Control::BorderThicknessProperty(),
        box_value(L"10.5") });

    coloredButtonStyle.Setters().Append(Setter{
        Control::BorderBrushProperty(),
        SolidColorBrush(Colors::Black()) });

    coloredButtonStyle.Setters().Append(Setter{
        Control::FontSizeProperty(),
        box_value(50) });

    coloredButtonStyle.Setters().Append(Setter{
        Control::ForegroundProperty(),
        SolidColorBrush(Colors::White()) });

    coloredButtonStyle.Setters().Append(Setter{
        FrameworkElement::MarginProperty(),
        box_value(L"10, 10, 0, 0") });

    coloredButtonStyle.Setters().Append(Setter{
        Control::BackgroundProperty(),
        GenerateGradient() });
}

按预期应用了按钮的边距,边框宽度和颜色,字体大小,颜色和背景渐变。

result

© www.soinside.com 2019 - 2024. All rights reserved.