WPF进度条动画速度

WPF进度条动画速度,wpf,progress-bar,Wpf,Progress Bar,我注意到WPF进度条和WinForms进度条完全填充所需的时间有所不同 完全填充在表单和WPF中将值设置为100时,可以注意到WinForms会平滑地填充条,而WPF会立即填充条 我想知道我们是否可以在模板中编辑一个属性来更改它 希望我能说清楚,如果有人愿意,我也可以发布视频 编辑 注意到我所说的区别了吗 编辑2 用计时器填充进度条 using System; using System.Windows; using System.Windows.Controls; using System.Wi

我注意到WPF进度条和WinForms进度条完全填充所需的时间有所不同

完全填充在表单和WPF中将值设置为100时,可以注意到WinForms会平滑地填充条,而WPF会立即填充条

我想知道我们是否可以在模板中编辑一个属性来更改它

希望我能说清楚,如果有人愿意,我也可以发布视频

编辑

注意到我所说的区别了吗

编辑2

用计时器填充进度条

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace WpfApplication2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            this.Title = "WPF Progress Bar Demo";
        }

        private void fill(int from, int to)
        {
            Duration duration = new Duration(TimeSpan.FromSeconds(0.5));
            DoubleAnimation doubleanimation = new DoubleAnimation(from, to, duration);
            progb.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
        }

        private void fill_Click(object sender, RoutedEventArgs e)
        {
            fill(0, 100);
        }
    }
}
这样行吗?在任何地方都行吗

请随意改变它


谢谢。

这个想法是进度条报告实际进度,而不是时间流逝。它不打算成为一个动画,只是表明一些事情正在发生

基本原则是将
Value
绑定到DataContext类上的属性,并在出现进度里程碑时更新该值

您可以使用计时器以指定的速率填充-以下是一个示例:

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ProgressBar Value="{Binding Path=ProgressValue}"></ProgressBar>
</Grid>

以及守则:

  public partial class MainWindow : Window, INotifyPropertyChanged
{
    Timer timer;
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        timer = new Timer();
        timer.Interval = 1000;
        timer.Elapsed += new ElapsedEventHandler(t_Elapsed);
        timer.Start();
    }

    void t_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (this._progressValue < 100)
            this.ProgressValue = _progressValue + 10;
        else
        {
            timer.Stop();
            timer.Dispose();
        }
    }

    private double _progressValue;
    public double ProgressValue
    {
        get { return _progressValue; }
        set
        {
            _progressValue = value;
            RaisePropertyChanged("ProgressValue");
        }
    }

    private void RaisePropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
public分部类主窗口:窗口,INotifyPropertyChanged
{
定时器;
公共主窗口()
{
初始化组件();
this.DataContext=this;
定时器=新定时器();
计时器。间隔=1000;
timer.appeased+=新的ElapsedEventHandler(t_appeased);
timer.Start();
}
无效时间(对象发送方,ElapsedEventArgs e)
{
如果(该值小于100)
this.ProgressValue=_ProgressValue+10;
其他的
{
timer.Stop();
timer.Dispose();
}
}
私人双重价值;
公共双重价值
{
获取{return\u progressValue;}
设置
{
_价值=价值;
RaisePropertyChanged(“ProgressValue”);
}
}
私有void RaisePropertyChanged(字符串propName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新PropertyChangedEventArgs(propName));
}
公共事件属性更改事件处理程序属性更改;
}

看起来只有WPF进度条有问题(或没有问题)…另一个用户报告了它

  • WPF控件,确切地说是进度条,在 复制当我测试复制一个大文件时,完整的GUI只是 完全冻结。进度条运行不平稳。只是 从0跳到100
  • 通过添加扩展方法解决了此问题:

     //Your Code
        pbBar.Value = some_value;
        pbBar.Refresh();
     //Your Code
    
    public static class ExtensionMethods
    {
        private static Action EmptyDelegate = delegate() { };
        public static void Refresh(this UIElement uiElement)
        {
            uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
        }
    
        public static void RefreshInput(this UIElement uiElement)
        {
            uiElement.Dispatcher.Invoke(DispatcherPriority.Input, EmptyDelegate);
        }
    }
    
    设置值后调用Refresh()方法解决了问题

    但是,我发现即使在应用refresh()方法之后,每次运行时进度条都会跳转(从不同的值)

    使用backgroundworker和reportprogress可以得到准确的结果,而不会出现“跳转”现象。

    请参阅我在上的答案


    它类似于扩展方法,但使用了一种行为,以便可以将进度条与报告进度的内容分离。:)

    在什么操作系统上,使用什么WPF样式(如果有的话)?Windows7(这是否意味着它不能在XP/Vista上工作?)。。。我现在对样式一无所知,所以不知道。@SLaks这些细节都无关紧要。@Greg:我想是的;我只是想确定他说的是哪部动画片。看起来WPF progressbar没有这样做。在描述中添加了一个视频。感谢您的回答。我不是在尝试根据时间更新进度,而是在WPF中更改填充动画时间。也许只是在我的电脑上…我加了一个视频链接,请看一下。嗯。。。WPF进度条没有填充“动画”-设置此值后,将根据
    值对其进行填充。你可以通过创建一个计时器来模拟这种影响,每半秒左右增加10次填充量-这对你有用吗?抱歉,我花了一些时间来评论,但我更新了关于使用计时器建议填充的问题。这就是你的意思吗?是的,看起来会有用的。技术上没有问题,但最好根据实际进度而不是时间更新进度指标。