Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
何时启动WPF进度条_Wpf_Visual Studio 2010_C# 4.0 - Fatal编程技术网

何时启动WPF进度条

何时启动WPF进度条,wpf,visual-studio-2010,c#-4.0,Wpf,Visual Studio 2010,C# 4.0,我希望我的应用程序在执行某些组件检查时显示正在运行的进度条。然而,由于我缺乏桌面应用程序编程和WPF方面的知识,我找不到合适的地方 我试图在窗口加载(),ContentRendered()期间显示递增的进度条,但没有成功 它只显示进度条的最终状态,而不是显示进度条的增加 这是密码 public partial class Loading : Window { public Loading() { InitializeComponent(); Set

我希望我的应用程序在执行某些组件检查时显示正在运行的进度条。然而,由于我缺乏桌面应用程序编程和WPF方面的知识,我找不到合适的地方

我试图在
窗口加载()
ContentRendered()
期间显示递增的进度条,但没有成功

它只显示进度条的最终状态,而不是显示进度条的增加

这是密码

public partial class Loading : Window
{
    public Loading()
    {
        InitializeComponent();
        SetProgressBar();
        this.Show();
        CheckComponents();
    }

    private void CheckComponents()
    {
        System.Threading.Thread.Sleep(3000);

        CheckProductionDBConnection();
        pgrsBar.Value = 30;

        System.Threading.Thread.Sleep(3000);
        CheckInternalDBConnection();
        pgrsBar.Value = 60;

        System.Threading.Thread.Sleep(3000);
        CheckProductionPlanning();
        pgrsBar.Value = 90;

        //MainWindow mainWindow = new MainWindow();
        //mainWindow.Show();
    }

    private void SetProgressBar()
    {
        pgrsBar.Minimum = 0;
        pgrsBar.Maximum = 100;
        pgrsBar.Value = 0;
    }
//more code down here...

我应该将
CheckComponents()
方法放在哪里?

您可以将此代码放在订阅了
激活的
事件的事件处理程序中。这样做的一个缺点是,每次窗口在失去焦点后接收焦点时,都会触发
激活的
事件。为了解决这个问题,您可以在事件处理程序中做的第一件事是取消订阅
激活的
事件,这样您的代码只在第一次激活窗口时执行

如果不希望延迟阻塞主线程,还需要将此工作卸载到工作线程。如果这样做,则必须调用调用来更新进度条的值

下面是一些示例代码,可以帮助您开始:

public Loader()
{
  InitializeComponent();
  SetProgressBar();

  this.Activated += OnActivatedFirstTime;
}

private void OnActivatedFirstTime(object sender, EventArgs e)
{
  this.Activated -= this.OnActivatedFirstTime;

  ThreadPool.QueueUserWorkItem(x =>
  {
    System.Threading.Thread.Sleep(3000);

    CheckProductionDBConnection();
    this.Dispatcher.BeginInvoke(new Action(() => pgrsBar.Value = 30));

    System.Threading.Thread.Sleep(3000);
    CheckInternalDBConnection();
    this.Dispatcher.BeginInvoke(new Action(() => pgrsBar.Value = 60));

    System.Threading.Thread.Sleep(3000);
    CheckProductionPlanning();
    this.Dispatcher.BeginInvoke(new Action(() => pgrsBar.Value = 90));
  });
}

private void SetProgressBar()
{
  pgrsBar.Minimum = 0;
  pgrsBar.Maximum = 100;
  pgrsBar.Value = 0;
}

听起来不像我想象的那么简单。谢谢你。我只是注意到WPF和普通的windows窗体有很大的不同。我又好奇了。。这叫什么<代码>this.Dispatcher.BeginInvoke(新操作(()=>pgrsBar.Value=90))是lambda的东西吗?这很有效!只需更改
CheckProductionDBConnection()
到this
this.Dispatcher.BeginInvoke(新操作(()=>CheckProductionDBConnection())以避免线程所有权错误。其他支票也是一样。。功能。再次感谢!