B_pati
B_pati

Reputation: 69

Show Splash Screen & Progress Bar with Percentage using MVVM & WPF

I need to show Splash Screen with Image & progress bar.

  1. In my application start up i have the code as below to show the main window.

         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. In my main viewmodel i have code as below

    class MainviewModel : viewmodelbase { rivate string _message; private object content; private readonly BackgroundWorker worker; private readonly 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 has one user control with progress bar and one splash main window. when my long running operation is completed , my Main window(main application) is opened. //User Control

            <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/>
    

My Problem is

ProgressChangedevent is not firing and % completion is not showing up in the text block either. Please help

Upvotes: 1

Views: 7062

Answers (1)

paparazzo
paparazzo

Reputation: 45096

You have not registered a complete handler and you are not calling progress properly. This sample from MSDN covers it all.

BackGroundWorker

Upvotes: 1

Related Questions