WPFでプログラムの進行状況を表示する方法は?

WPF内で、ProgressBarコントロールを使用してプログラムの実行進捗を表示することができます。

最初に、XAMLファイルにProgressBarコントロールを追加します。

<ProgressBar x:Name="progressBar" Width="200" Height="20" Minimum="0" Maximum="100" Value="0"/>

その後、ProgressBarの進捲を更新するために、コード内でDispatcherTimerを使用します。

using System.Windows;
using System.Windows.Threading;

public partial class MainWindow : Window
{
    private DispatcherTimer timer;
    private int progress;

    public MainWindow()
    {
        InitializeComponent();

        // 初始化进度为0
        progress = 0;

        // 创建一个DispatcherTimer,每隔一段时间更新进度
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(0.1);
        timer.Tick += Timer_Tick;
        timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        // 更新进度
        progress += 1;
        progressBar.Value = progress;

        // 当进度达到100时,停止计时器
        if (progress >= 100)
        {
            timer.Stop();
        }
    }
}

上記のコードでは、DispatcherTimerを使用して、進捗バーの値を0.1秒ごとに更新し、進捗が100に達したらタイマーを停止します。Timer_Tickイベントハンドラでは、進捗値を増やし、それをProgressBarのValueプロパティに割り当てることで、進捗バーの表示を更新しています。

bannerAds