Raise method not available in descendent

Topics: Prism v4 - Silverlight 4
May 2, 2012 at 1:11 PM

I'm following the MVVM RI example application. The QuestionnaireViewModel shows an example of how to open a popup child window which I followed in my app and have working. The next step is to move that code into an ancestor called ViewModelBase so that I don't have to duplicate it in every viewmodel. The problem is the Raise method isn't available to my descendent viewmodel because the class that has the method is private in the ViewModelBase class. How do I make this work?

 

Here's my ViewModelBase class...

    public abstract class ViewModelBase :NotificationObject, IViewModelBase
    {
        protected ViewModelBase()
        {
            _submitErrorInteractionRequest = new InteractionRequest<Notification>();
            _cancelConfirmationInteractionRequest = new InteractionRequest<Confirmation>();

           
        }

        #region PRIVATE PROPERTIES
       
        private bool _isBusy;
        private string _errorMessages;

        private readonly InteractionRequest<Confirmation> _cancelConfirmationInteractionRequest;
        private readonly InteractionRequest<Notification> _submitErrorInteractionRequest;
       
        #endregion
       
        #region PUBLIC PROPERTIES
        public virtual bool IsBusy
        {
            get
            {
                return _isBusy;
            }
            set
            {
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }

        public virtual string ErrorMessages
        {
            get
            {
                return _errorMessages;
            }
            set
            {
                _errorMessages = value;
                RaisePropertyChanged("ErrorMessages");
            }
        }

        public IInteractionRequest CancelConfirmationInteractionRequest
        {
            get
            {
                return _cancelConfirmationInteractionRequest;
            }
        }

        public IInteractionRequest SubmitErrorInteractionRequest
        {
            get
            {
                return _submitErrorInteractionRequest;
            }
        }

        #endregion
    }

 

Here's my interface...

    public interface IViewModelBase
    {
        bool IsBusy { get; set; }
        string ErrorMessages { get; set; }
        IInteractionRequest CancelConfirmationInteractionRequest { get; }
        IInteractionRequest SubmitErrorInteractionRequest { get; }
    }

 

Here's a code snippet in the descendent that I need to fix...

            if (lo.HasError)
            {
                lo.MarkErrorAsHandled();

                // THIS NO LONGER WORKS BECAUSE _submitErrorInteractionRequest

               // IS PRIVATE IN VIEWMODELBASE
                _submitErrorInteractionRequest.Raise(
                    new Notification { Content = lo.Error, Title = "Error loading data" }, se => { });
            }

 

This is my first post here as I learn so pardon any errors.

Thanks,

Mark

Developer
May 2, 2012 at 5:44 PM

Hi Mark,

As both the code for the ViewModelBase class and the child view model classes are defined by you, you can easily modify them according to your requirements. In this case, a possible solution could be to declare the _submitErrorInteractionRequest member of the ViewModelBase class as protected instead of private. However, as it can be seen in the code snippet posted above, your ViewModelBase class already has a SubmitErrorInteractionRequest public property which returns the value of the aforementioned member. Therefore, I believe that the best approach could be to change the following code in your child view models:

_submitErrorInteractionRequest.Raise(
                    new Notification { Content = lo.Error, Title = "Error loading data" }, se => { });

... with the following one:

this.SubmitErrorInteractionRequest.Raise(
                    new Notification { Content = lo.Error, Title = "Error loading data" }, se => { });

As a side note, as this is not strictly related to Prism but to C#, I believe you might find better support regarding this kind of topics in the C# forums:

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini

May 3, 2012 at 1:08 PM

Hi Damian,

Thank you for your response. I did change the declaration of _submitErrorInteractionRequest to protected as a workaround but I don't think that was the original intent of the PRISM folks.

My first thought was to try using the public property as you suggested but it doesn't have a Raise method. It is defined in PRISM as IInteractionRequest whereas _submitErrorInteractionRequest is defined in PRISM as InteractionRequest<Notification>.

Thanks again for your insight,

Mark