C# AvaloniaUI ProgressBar用法

AvaloniaUI ProgressBar 基本样式和用法

在AvaloniaUI中使用ProgressBar控件时,可以通过以下代码定义基本样式和绑定数据:

csharp 复制代码
<ProgressBar 
    Minimum="0" 
    Maximum="100" 
    Value="{Binding ProgressValue}" 
    Width="200" 
    Height="20"/>

对应的ViewModel需要包含进度值属性:

csharp 复制代码
private double _progressValue;
public double ProgressValue
{
    get => _progressValue;
    set => this.RaiseAndSetIfChanged(ref _progressValue, value);
}

自定义进度条样式

通过Avalonia的样式系统可以修改进度条外观:

xml 复制代码
<Style Selector="ProgressBar">
    <Setter Property="Background" Value="#E0E0E0"/>
    <Setter Property="Foreground" Value="#0078D7"/>
    <Setter Property="Height" Value="10"/>
    <Setter Property="CornerRadius" Value="5"/>
</Style>

<Style Selector="ProgressBar:disabled">
    <Setter Property="Opacity" Value="0.6"/>
</Style>

动画效果实现

为进度变化添加平滑动画:

xml 复制代码
<Style Selector="ProgressBar">
    <Setter Property="Template">
        <ControlTemplate>
            <Border Background="{TemplateBinding Background}"
                    CornerRadius="{TemplateBinding CornerRadius}">
                <Rectangle Name="PART_Track"
                          Fill="{TemplateBinding Foreground}"
                          CornerRadius="{TemplateBinding CornerRadius}"
                          Width="{TemplateBinding Value, Converter={StaticResource PercentageConverter}}"/>
            </Border>
        </ControlTemplate>
    </Setter>
    <Style.Animations>
        <Animation Duration="0:0:0.3" Easing="Linear">
            <KeyFrame Cue="0%">
                <Setter Property="Opacity" Value="0.5"/>
            </KeyFrame>
            <KeyFrame Cue="100%">
                <Setter Property="Opacity" Value="1"/>
            </KeyFrame>
        </Animation>
    </Style.Animations>
</Style>

不确定状态进度条

处理未确定进度的场景:

xml 复制代码
<ProgressBar IsIndeterminate="True" 
             Width="200"
             Height="8">
    <ProgressBar.Styles>
        <Style Selector="ProgressBar:indeterminate">
            <Setter Property="Foreground" Value="#FF5722"/>
        </Style>
    </ProgressBar.Styles>
</ProgressBar>

进度文本显示

在进度条上叠加文本显示百分比:

xml 复制代码
<Grid Width="200" Height="24">
    <ProgressBar Value="{Binding ProgressValue}"
                 Minimum="0" 
                 Maximum="100"/>
    <TextBlock Text="{Binding ProgressValue, StringFormat={}{0}%}"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"/>
</Grid>

垂直进度条实现

通过旋转实现垂直方向进度条:

xml 复制代码
<ProgressBar Width="20" 
             Height="100"
             Rotation="-90"
             HorizontalAlignment="Left"
             Value="{Binding ProgressValue}"/>

注意事项

  1. 确保Value属性始终在MinimumMaximum范围内,否则会引发异常
  2. 使用数据绑定时实现INotifyPropertyChanged接口确保UI更新
  3. 复杂样式建议放在资源字典中复用
  4. 不确定模式(IsIndeterminate)与确定模式互斥
  5. 高精度进度建议使用double类型而非int
相关推荐
雨落倾城夏未凉2 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫3 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫4 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6254 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902114 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠5 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫7 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech7 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf9 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6259 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#