XAML 如何在WPF中更改不确定进度条动画的速度和宽度?

eqfvzcg8  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(153)

关于IsIndeterminate设置为true时的WPF进度条,我个人认为动画太快了。
我认为放慢动画的速度,也许使内部栏更宽将有助于缓解这个问题。
是否可以自定义此选项?

编辑:添加的代码

.xaml格式

<Window x:Class="IndeterminateProgressBarTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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"
        xmlns:local="clr-namespace:IndeterminateProgressBarTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="100" Width="700">
    <Grid>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup Name="Type">
                <VisualState Name="Indeterminate">
                    <Storyboard RepeatBehavior="Forever">
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Animation" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                        </DoubleAnimationUsingKeyFrames>
                        <PointAnimationUsingKeyFrames Storyboard.TargetName="Animation" Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)">
                            <EasingPointKeyFrame KeyTime="0" Value="0,0"/>
                            <EasingPointKeyFrame KeyTime="0:0:0" Value="0,0"/>
                            <EasingPointKeyFrame KeyTime="1:0:0" Value="0,0"/>
                        </PointAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        <ProgressBar Name="progressBar" Width="600" Height="25" IsIndeterminate="True"/>
    </Grid>
</Window>

.cs格式

namespace IndeterminateProgressBarTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            VisualStateManager.GoToElementState(progressBar, "Indeterminate", true);
        }
    }
}
oipij1gg

oipij1gg1#

您可以通过编辑名为Indeterminate的VisualState来自定义Indeterminate动画。以下是VistalState在当前默认样式ProgressBar中的动画。

<VisualState x:Name="Indeterminate">
    <Storyboard RepeatBehavior="Forever">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Animation" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
            <EasingDoubleKeyFrame KeyTime="0" Value="0.25"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.25"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="0.25"/>
        </DoubleAnimationUsingKeyFrames>
        <PointAnimationUsingKeyFrames Storyboard.TargetName="Animation" Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)">
            <EasingPointKeyFrame KeyTime="0" Value="-0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:1" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:2" Value="1.5,0.5"/>
        </PointAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

分别更改2nd和3rd KeyTime以更改动画的速度。进一步的自定义由您决定。

相关问题