Show Splash Screen&使用MVVM和&amp ;;进度条的百分比WPF

时间:2012-06-04 21:33:32

标签: wpf c#-4.0 mvvm-light

我需要使用Image&amp ;;显示启动画面进度条。

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

    }

    我的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}"  />
    

    // SplashWindow

      <localView:usercontrol/>
    
  3. 我的问题是

    ProgressChangedevent未触发,%完成也未显示在文本块中。请帮忙

1 个答案:

答案 0 :(得分:1)

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

BackGroundWorker