WPF 进度条动画速度

发布于 2024-12-04 01:21:04 字数 1192 浏览 1 评论 0原文

我注意到 WPF 进度栏和 WinForms 进度栏完全填满所需的时间存在差异。

完全填充,就像在 Form 和 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);
        }
    }
}

可以吗?它可以在任何地方使用吗?

请随意更改它。

谢谢。

I've noticed that there is a difference in the time it takes for a WPF Progress Bar and a WinForms Progress Bar to fill completely.

Fill completely as in set the Value to 100 in both Forms and WPF, one can notice that WinForms fills the bar smoothly whereas the WPF fills it instantly.

I wanted to know if there is a property that we can edit in the templates to change that.

Hope I made it clear, I can post a video too if anyone wants.

EDIT

Here's a video of what I'm talking about, notice the difference ?

EDIT 2

Filling the progress bar with a timer ?

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);
        }
    }
}

Is that OK and will it work anywhere ?

Feel free to change it.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野の 2024-12-11 01:21:04

这个想法是进度条报告实际进度 - 而不是经过的时间。它并不是仅仅指示正在发生某事的动画。

基本原则是将 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;
}

The idea is that a progress bar reports actual progress - not time elapsed. It's not intended to be an animation that just indicates something is happening.

The basic principle is that you bind Value to a property on your DataContext class, and update that value whenever a progress milestone occurs.

You can make it fill at a specified rate using a timer - here is an example:

<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>

And the code:

  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;
}
放手` 2024-12-11 01:21:04

请参阅我的答案 如何更新进度条使其平滑增加?

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

See my answer on How to update a progress bar so it increases smoothly?

It's similar to the extension method, but uses a behavior so that you can decouple the progress bar from the thing that's reporting progress. :)

风追烟花雨 2024-12-11 01:21:04

看起来只有 WPF 进度条有问题(或没有)...另一位用户报告了它 此处

  1. 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()方法解决了这个问题。

但是,我发现即使在应用了刷新()方法之后,进度条也会在每次运行时跳跃(从不同的值)。

使用backgroundworkerreportprogress给出准确的结果,没有“跳跃”。

It looks like it's a problem (or not) with only WPF progress bar...another user reported it here

  1. WPF Control, exactly progress bar, does not update itself when
    copying When I test to copy a big file, the complete GUI just
    completely freezes. The progress bar doesn’t run smoothly. It just
    jumps from 0 to 100.

It was solved by adding an extension method:

 //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);
    }
}

Calling the Refresh() method after setting the value solved the issue.

But, what I found was even after applying the refresh() method, the progress bar jumps on each run (from different values).

Using a backgroundworker and reportprogress gives the exact result with no "jumps".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文