Monthly Archives: July 2015

A simple cross platform status bar micro service

These days, with all the buzz and best practices around micro-services, Clean Code and KISS, one should be proud of themselves after creating a simple, small and cross platform micro-service to further advocate the power of these patterns and demonstrate how easy developing robust, maintainable and beautiful code can be. Any complex problem can be broken down into small manageable chunks.

Requirement was to add a status bar to a WPF application to indicate progress of some background calculations. I wanted to use MVVM and also wanted to be able to access the main application’s status bar from anywhere in the application, mostly from the ViewModels. ViewModels are currently being injected services from the Unity container so I thought, hey, why not create a simple StatusBarService that can be bound to a StatusBar control, which the ViewModel can the control from there? I did something similar year ago but this approach is more mature and greatly simplified.

The service in its entirety as follows:

 public class StatusBarService : IStatusBarService, INotifyPropertyChanged
    {
        public StatusBarService()
        {
            ProgressBarVisibility = Visibility.Hidden;
            Name = null;
            Message = "Ready";
        }

        public string Name { get; private set; }

        private bool _isIndeterminate;
        public bool IsIndeterminate
        {
            get { return _isIndeterminate; }
            set
            {
                _isIndeterminate = value;
                OnPropertyChanged();

                if (!_isIndeterminate)
                {
                    ProgressBarVisibility = Visibility.Hidden;
                }
                else
                {
                    ProgressBarVisibility = Visibility.Visible;
                }
            }
        }

        public void StartIsIndeterminate(string message)
        {
            IsIndeterminate = true;
            Message = message;
        }

        public void StopIsIndeterminate(string message)
        {
            IsIndeterminate = false;
            Message = message;
        }

        private string _message;
        public string Message
        {
            get { return _message; }
            set
            {
                _message = value;
                OnPropertyChanged();
            }
        }

        private Visibility _progressBarVisibility;
        public Visibility ProgressBarVisibility
        {
            get { return _progressBarVisibility; }
            set
            {
                _progressBarVisibility = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

It is cross platform and does not care how you use it.

You can register it with your IoC container and modify your ViewModel constructors to be injected an instance of IStatusBarService. Once you have access in your ViewModel, you can control any Statusbar as you see fit. Only caveat is ensuring the StatusBarService is bound to the DataContext of the StatusBar it should be controlling.

Happy Coding.