在从一个页面导航到另一个页面时需要加载GIF或进度条

时间:2017-08-05 09:31:16

标签: c# wpf

第一页

  private void Button_Click(object sender, RoutedEventArgs e)
    {
        Page1 p1 = new Page1();
        this.NavigationService.Navigate(p1);

    }

第二页(代码中指定的第1页)

        public Page1()
    { 
        InitializeComponent();
        Thread.Sleep(10000);
    }

在点击按钮时从第3页导航到第1页时获取加载图标或进度条。

只要用户点击第3页上的按钮,此页面就会挂起,直到第1页处理后台数据

代码将更可取

1 个答案:

答案 0 :(得分:-1)

我在codeproject找到了这个例子,你看一下这个例子。我希望这是有帮助的 如何在c#.net windows应用程序中将一个页面中的进度条设置为另一个页面加载?

This gets asked almost daily so I've written this short demonstration code to demonstrate how to use a System.ComponentModel.BackgroundWorker in combination with a System.Windows.Forms.ProgressBar. All the code is commented so the flow/steps should be easy to understand.

Just drop a ProgressBar (progressBar1) and a BackgroundWorker (backgroundWorker1) onto your form and copy and paste this code to see it in action.
Hide   Shrink    Copy Code
public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
    Shown += new EventHandler(Form1_Shown);

    // To report progress from the background worker we need to set this 
  property
    backgroundWorker1.WorkerReportsProgress = true;
    // This event will be raised on the worker thread when the worker starts
    backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
    // This event will be raised when we call ReportProgress
    backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}
void Form1_Shown(object sender, EventArgs e)
{
    // Start the background worker
    backgroundWorker1.RunWorkerAsync();
}
// On worker thread so do our thing!
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // Your background task goes here
    for (int i = 0; i <= 100; i++)
    {
        // Report progress to 'UI' thread
        backgroundWorker1.ReportProgress(i);
        // Simulate long task
        System.Threading.Thread.Sleep(100);
    }
}
// Back on the 'UI' thread so we can update the progress bar
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // The progress percentage is a property of e
    progressBar1.Value = e.ProgressPercentage;
}
}