XAML 如何修改裤子

bfhwhh0e  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(132)

Using FluentAvalonia styles for Avalonia in .NET.
Let's say I want to edit the default style to set the button background to AccentColor3 and on hover AccentColor2. How do I do that?
First, setting this style works; but setting ThemeAccentBrush3 doesn't. How do I set set the accent in a way that works for both dark and light themes?

<Style Selector="Button">
    <Setter Property="Background" Value="{DynamicResource SystemAccentColorDark3}" />
</Style>

Second, I figured that the best approach is to create an Avalonia Dictionary file

<ResourceDictionary xmlns="https://github.com/avaloniaui"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StaticResource x:Key="ButtonBackground" ResourceKey="SystemAccentColorDark3" />
    <StaticResource x:Key="ButtonBackgroundPointerOver" ResourceKey="SystemAccentColorDark2" />
</ResourceDictionary>

And then reference in App.axaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceInclude Source='avares://Common.Avalonia.App/Styles/DarkResources.axaml'/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

However, styles are loaded further down, and thus don't exist yet.

<Application.Styles>
    <sty:FluentAvaloniaTheme />

Thus...

Static resource 'SystemAccentColorDark3' not found.

What's the cleanest solution?

mzaanser

mzaanser1#

<Style Selector="Button">
    <Setter Property="Background" Value="{DynamicResource ButtonBackground}" />
</Style>

应该对你的背景值起作用,那么:

<Style Selector="Button:pointerover">
    <Setter Property="Background" Value="{DynamicResource ButtonBackgroundPointerOver}" />
</Style>

对于您的mouseover,控件有一种使用pseudoclasses表示的“状态”,在运行时应用,您可以在样式中将其作为目标,在本例中为Button:pointerover

pxq42qpu

pxq42qpu2#

通过将“accent”样式转换为不同的语法来覆盖资源,我成功地获得了默认样式为.35不透明度的“accent”样式......要完成此转换需要相当复杂的引用,但它很有效。
需要在应用程序.xaml中的FluentAvalonia之后插入词典
此外,要查看可用资源的列表,我可以运行FluentAvalonia示例应用程序,该应用程序有一个页面,允许可视化资源和颜色的整个列表。
作为绑定适应主题的说明:StaticResource只运行一次,DynamicResource跟踪更改。要覆盖资源键,需要StaticResource

<ResourceDictionary xmlns="https://github.com/avaloniaui"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:flu="clr-namespace:FluentAvalonia.UI.Controls;assembly=FluentAvalonia">

    <SolidColorBrush x:Key="ButtonBackgroundApp" Color="{DynamicResource SystemAccentColorLight2}" Opacity=".35" />
    <StaticResource x:Key="ButtonBackground" ResourceKey="ButtonBackgroundApp" />

    <LinearGradientBrush x:Key="ButtonBorderBrushApp" StartPoint="0,0" EndPoint="0,3" Opacity=".35">
        <LinearGradientBrush.GradientStops>
            <GradientStop Offset="0.33" Color="{StaticResource ControlStrokeColorOnAccentSecondary}" />
            <GradientStop Offset="1.0" Color="{StaticResource ControlStrokeColorOnAccentDefault}" />
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>
    <StaticResource x:Key="ButtonBorderBrush" ResourceKey="ButtonBorderBrushApp" />

    <SolidColorBrush x:Key="ButtonBackgroundPointerOverApp" Color="{DynamicResource SystemAccentColorLight2}"
                     Opacity=".30" />
    <StaticResource x:Key="ButtonBackgroundPointerOver" ResourceKey="ButtonBackgroundPointerOverApp" />

    <LinearGradientBrush x:Key="ButtonBorderBrushPointerOverApp" StartPoint="0,0" EndPoint="0,3" Opacity=".35">
        <LinearGradientBrush.GradientStops>
            <GradientStop Offset="0.33" Color="{StaticResource ControlStrokeColorOnAccentSecondary}" />
            <GradientStop Offset="1.0" Color="{StaticResource ControlStrokeColorOnAccentDefault}" />
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>
    <StaticResource x:Key="ButtonBorderBrushPointerOver" ResourceKey="ButtonBorderBrushPointerOverApp" />

    <SolidColorBrush x:Key="ButtonBackgroundPressedApp" Color="{DynamicResource SystemAccentColorLight2}" Opacity=".27" />
    <StaticResource x:Key="ButtonBackgroundPressed" ResourceKey="ButtonBackgroundPressedApp" />

    <SolidColorBrush x:Key="ButtonBorderBrushPressedApp" Color="{DynamicResource ControlFillColorTransparent}"
                     Opacity=".35" />
    <StaticResource x:Key="ButtonBorderBrushPressed" ResourceKey="ButtonBorderBrushPressedApp" />

    <SolidColorBrush x:Key="ButtonBackgroundDisabledApp" 
                     Color="{DynamicResource TextOnAccentFillColorDisabled}" Opacity=".35" />
    <StaticResource x:Key="ButtonBackgroundDisabled" ResourceKey="ButtonBackgroundDisabledApp" />

    <SolidColorBrush x:Key="ButtonBorderBrushDisabledApp"
                     Color="{DynamicResource ControlFillColorTransparent}" Opacity=".35" />
    <StaticResource x:Key="ButtonBorderBrushDisabled" ResourceKey="ButtonBorderBrushDisabledApp" />
</ResourceDictionary>

相关问题