简体   繁体   中英

How to use MVVM Light Messenger in Windows Phone?

I am trying to learn how to use the messenger class in MVVM Light but nothing ever get sent.

 public class MainViewModel : ViewModelBase
    {
        private readonly INavigationService navigationService = null;
        public MainViewModel(INavigationService navigationService)
        {
            this.navigationService = navigationService;
            SecondPgCmd = new RelayCommand(() => SecondPg());

        }

        private void SecondPg()
        {

            Messenger.Default.Send<string>("my message");
            navigationService.NavigateTo("/Views/SecondPg.xaml");
        }


        public RelayCommand SecondPgCmd
        {
            get;
            private set;
        } 

    }


    public class SecondVm : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the SecondVm class.
        /// </summary>
        public SecondVm()
        {
            Messenger.Default.Register<string>(this, x => MyProperty = x);
        }

           /// <summary>
        /// The <see cref="MyProperty" /> property's name.
        /// </summary>
        public const string MyPropertyPropertyName = "MyProperty";

        private string myProperty = "";

        /// <summary>
        /// Sets and gets the MyProperty property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string MyProperty
        {
            get
            {
                return myProperty;
            }

            set
            {
                if (myProperty == value)
                {
                    return;
                }

                RaisePropertyChanging(() => MyProperty);
                myProperty = value;
                RaisePropertyChanged(() => MyProperty);
            }
        }
    }

  static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            if (ViewModelBase.IsInDesignModeStatic)
            {

            }
            else
            {

            }

            SimpleIoc.Default.Register<INavigationService, NavigationService>();

            SimpleIoc.Default.Register<MainViewModel>();
            SimpleIoc.Default.Register<SecondVm>();
        }

Try passing true as the first Parameter when registering your view models, which register for messages. This way, they will be created immediately and register for messages right from the beginning, not when the views are called.

SimpleIoc.Default.Register<SecondVm>(true);

See this great article to learn more about messaging, where this problem is also being discussed.

Messaging is used to pass parameters from one service to another. In this case when you want to save data for later use, it would be best to keep it somewhere. The proposed solution by @sibbl might work, but you are needlessly creating a view model that is not used (yet) and the solution doesn't scale.

Since you already have SimpleIoc at hand, simply create a new object and put it inside. Lke this:

class ImportantMessageService
{
    public string Message { get; set; }
}


// in locator
SimpleIoc.Default.Register<ImportantMessageService>();


// in page 1
var service = SimpleIoc.Default.GetInstance<ImportantMessageService>();
service.Message = "Hello world";


// in page 2
var service = SimpleIoc.Default.GetInstance<ImportantMessageService>();
MessageBox.Show(service.Message);

This way all participants in the communication use central service where they keep data. This scales well because in case you never go to the second page, the data is simply unused. The only thing you need to worry about here is tombstoning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM