1

我需要显示带有图像和进度条的启动画面。

  1. 在我的应用程序启动中,我有如下代码来显示主窗口。

         SplashScreenWindowViewModel vm = new SplashScreenWindowViewModel(); 
         AutoResetEvent ev = new AutoResetEvent(false); 
          Thread uiThread = new Thread(() =>
         {
            vm.Dispatcher = Dispatcher.CurrentDispatcher;
            ev.Set();
    
            Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate()
            {
                SplashScreenWindow splashScreenWindow = new SplashScreenWindow();
                splashScreenWindow = new SplashScreenWindow();
                splashScreenWindow.Show();
                splashScreenWindow.DataContext = vm;
                vm.InstigateWorkCommand.Execute(null);
    
            });
    
            Dispatcher.Run();
        });
          uiThread.SetApartmentState(ApartmentState.STA);
          uiThread.IsBackground = true;
          uiThread.Start();
          ev.WaitOne();
    
  2. 在我的主视图模型中,我的代码如下

    类 MainviewModel : viewmodelbase { rivate string _message; 私有对象内容;私有只读 BackgroundWorker 工作者;私有只读 ICommand instigateWorkCommand;

        public SplashScreenWindowViewModel()
        {
    
            this.instigateWorkCommand = new
             RelayCommand(() => this.worker.RunWorkerAsync(), () => !this.worker.IsBusy);
            this.worker = new BackgroundWorker { WorkerReportsProgress = true };           
            this.worker.DoWork += this.DoWork;
            this.worker.ProgressChanged += this.ProgressChanged;
            _message = "0 % completed";
    
        }
    
    
    
        public ICommand InstigateWorkCommand
        {
    
            get { return this.instigateWorkCommand; }
        }
    
        private double _currentProgress;
        public double CurrentProgress        
        {
            get { return this._currentProgress; }
            set
            {
                if (this._currentProgress != value)
                {
    
                    this._currentProgress = value;                   
                    RaisePropertyChanged("CurrentProgress");
                }
            }
        }
        private int _progressMax;
        public int ProgressMax
        {
            get { return this._progressMax; }
    
            set
            {
                if(this._progressMax != value)
                {
                  this._progressMax = value;
                  RaisePropertyChanged("ProgressMax");
                }
    
            }
        }
    
        private void ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
          this.CurrentProgress = e.ProgressPercentage;
    
    
        }
    
        private void DoWork(object sender, DoWorkEventArgs e)
        {
    
           // calling my long running operation
           DAL.dotimeconsumingcode();
           worker.ReportProgress((int)e.argument);
    
        }
    
    
    
    
        public string Message
        {
            get
            {
                return _message;
            }
            set
            {
                if (Message == value) return;
                _message = value;
                RaisePropertyChanged("Message");
            }
        }
    
    
        public object Content
        {
            get
            {
                return content;
            }
            set
            {
                if (Content == value) return;
                content = value;
    
                RaisePropertyChanged("Content");
            }
        }
    
        public Dispatcher Dispatcher
        {
            get;
            set;
        }
    

    }

    MY UI 有一个带有进度条的用户控件和一个启动主窗口。当我长时间运行的操作完成后,我的主窗口(主应用程序)被打开。//用户控制

            <ProgressBar Height="27" Value="{Binding CurrentProgress, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"  Margin="53,162,57,0" Name="progressBar"  Grid.Row="1"
                 Maximum="{Binding Path=ProgressMax, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"    Visibility="{Binding ProgressVisibility}"  />
    

    //启动窗口

      <localView:usercontrol/>
    

我的问题是

ProgressChangedevent 没有触发,并且 % 完成也没有显示在文本块中。请帮忙

4

1 回答 1

1

您尚未注册完整的处理程序,并且您没有正确调用进度。这个来自 MSDN 的示例涵盖了所有内容。

背景地勤工作者

于 2012-06-05T00:18:28.500 回答