ViewNotificationMessageAction

Coordinator
Jan 17, 2012 at 6:10 PM
Edited Jan 17, 2012 at 6:10 PM

The class:

 

    /// <summary>
    /// Extends the default MVVM Light message action
    /// with conventional message source and view ID properties.
    /// </summary>
    /// <typeparam name="TCallbackParameter">The type of the callback parameter.</typeparam>
    public class ViewNotificationMessageAction<TCallbackParameter> : NotificationMessageAction<TCallbackParameter>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ViewNotificationMessageAction&lt;TCallbackParameter&gt;"/> class.
        /// </summary>
        /// <param name="action">The action.</param>
        public ViewNotificationMessageAction(Action<TCallbackParameter> action) : base(string.Empty, action)
        {
        }

        /// <summary>
        /// Gets or sets the current view id.
        /// </summary>
        /// <value>
        /// The current view id.
        /// </value>
        public string CurrentViewId { get; set; }

        /// <summary>
        /// Gets or sets the source.
        /// </summary>
        /// <value>
        /// The source.
        /// </value>
        public MvvmLightMessageSource Source { get; set; }
    }

 

MvvmLightMessageSource:

    /// <summary>
    /// Enumerates the Sources of MVVM Light Messages
    /// </summary>
    public enum MvvmLightMessageSource
    {
        Unspecified,
        ApplicationCore,
        Model,
        View,
        ViewModel,
        ViewModelLocator
    }

Usage (sending):

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            Action<ObservableCollection<CalendarDateRange>> action =
            callbackData =>
            {
                this.WeekEndingDate.BlackoutDates.Clear();
                callbackData.ForEachInEnumerable(i =>
                {
                    this.WeekEndingDate.BlackoutDates.Add(i);
                });
            };

            var message = new ViewNotificationMessageAction<ObservableCollection<CalendarDateRange>>(action)
            {
                CurrentViewId = "OpenRentals",
                Source = MvvmLightMessageSource.View
            };

            Messenger.Default.Send(message);
        }

receiving:

            #region messaging:

            Messenger.Default.Register<ViewNotificationMessageAction<ObservableCollection<CalendarDateRange>>>(this,
                message =>
                {
                    if(message.Source != MvvmLightMessageSource.View) return;
                    if(message.CurrentViewId != "OpenRentals") return;

                    var callbackData = blackoutDates;
                    message.Execute(callbackData);
                });

            #endregion