总结几种 Avalonia 项目里常见的 Style 设置不生效故障

本文介绍了在AvaloniaUI中样式应用的常见问题,包括属性值覆盖样式、样式顺序的重要性以及在UserControl中正确设置样式的技巧,帮助开发者避免样式生效问题。
摘要由CSDN通过智能技术生成

在 Avalonia UI 中,样式(Styles)类似于 CSS 样式,通常用于根据控件的内容或在应用程序中的用途对控件进行样式化;例如,创建用于标题文本块的样式。新手在开发过程中,经常会遇到编写好了样式代码,但界面上却没有生效的情况。这里列举了几种常见的情形,帮大家避坑。

1、通过属性设置的值会覆盖样式中的设置。

<TextBlock Text="https://www.coderbusy.com" Background="Green">
   <TextBlock.Styles>
       <Style Selector="TextBlock">
           <Setter Property="Background" Value="Red"></Setter>
</Style>
   </TextBlock.Styles>
</TextBlock>
97b36f567817fd3e4201b400ddbee550.png

上述代码在 Style 中将背景色设置为了红色,但因为外侧的 TextBlock 被单独设置了 Background 为绿色,因此 Style 中的设置被覆盖并显示为绿色。

2、定义靠后的样式,会覆盖之前的设置。

<TextBlock Text="https://www.coderbusy.com" Classes="t1 t2">
   <TextBlock.Styles>
       <Style Selector="TextBlock">
           <Setter Property="Background" Value="Red"></Setter>
</Style>
       <Style Selector="TextBlock.t1">
           <Setter Property="Background" Value="Green"></Setter>
</Style>
       <Style Selector="TextBlock.t2">
           <Setter Property="Background" Value="Blue"></Setter>
</Style>
   </TextBlock.Styles>
</TextBlock>
392a278a36648fe1088022b87863f699.png

因为 TextBlock.t2 的定义最靠后,所以背景呈现为蓝色。如果将 TextBlock.t2 的定义与 TextBlock.t1 对调,则背景将会呈现为绿色。

<TextBlock Text="https://www.coderbusy.com" Classes="t1 t2">
   <TextBlock.Styles>
       <Style Selector="TextBlock">
           <Setter Property="Background" Value="Red"></Setter>
</Style>
       <Style Selector="TextBlock.t2">
           <Setter Property="Background" Value="Blue"></Setter>
</Style>
       <Style Selector="TextBlock.t1">
           <Setter Property="Background" Value="Green"></Setter>
</Style>
   </TextBlock.Styles>
</TextBlock>
7db02b5b5c1fc18717e42b7a6ecbf557.png

需要注意的是,本节所说的是“定义”的顺序,也就是 Style 块在代码中的顺序,而不是 Classes 属性中类名的顺序。笔者实测:改变 Classes 中类名的顺序并不影响最终的呈现结果。

是“定义”的顺序,而不是“引用”的顺序。

3、在 UserControl 中为自身设置样式时,选择器应该使用实际类型,而不是 UserControl 。

如果想要在鼠标滑过时,将 UserControl 的背景改为绿色,可以使用下面的代码:

<UserControl xmlns="https://github.com/avaloniaui"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
            xmlns:local="clr-namespace:CoderBusy"
            x:Class="CoderBusy.MyControl">
   <UserControl.Styles>
       <Style Selector="local|MyControl">
           <Setter Property="Background" Value="Transparent"></Setter>
</Style>
       <Style Selector="local|MyControl:pointerover">
           <Setter Property="Background" Value="Green"></Setter>
</Style>
   </UserControl.Styles>
</UserControl>

需要注意的是,这里的选择器中目标对象必须是实际类型:local|MyControl ,而不能是 UserControl 。否则样式不会生效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值