MessagingCenter.Subscribe call multiple times

geozedgeozed ARMember ✭✭

I'm using the MessagingCenter.Subscribe to display an alert and the message diplays multiple times. I'm using de MVVM pattern. So I subscribe in the viewmodel constructor. I've in other post the solution is clean the page and clean de viewmodels but i've couldn't make it work. Can anyone help. me?

    public MainPageViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;

        MessagingCenter.Subscribe<MainPageViewModel, NotificationDTO>(this, Constants.CHANGE_TEXT, (sender, arg) => {
            DisplayMessage(arg);
        });
    }

Best Answer

  • geozedgeozed AR ✭✭
    Accepted Answer

    @AlessandroCaliaro i want to communicate two view models using message center because I'm implementing the MVVM pattern. So, the codebehind of the pages only set the binding context, nothing else.

    I've resolved it displaying the message in the page that will be popped. And I've removed the message center.

    @VinayakGawas I've tried to use a boolean property but in my app there are many viewmodels that sends messages. So, I 've decided to remove it.

    Thanks for your answers!

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    The problem maybe on Send not on Subscribe
  • geozedgeozed ARMember ✭✭

    I subscribe in the viewmodel constructor in the first view model and I send in a command in the second view model:

        public MainPageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
    
            MessagingCenter.Subscribe<MainPageViewModel, NotificationDTO>(this, Constants.CHANGE_TEXT, (sender, arg) => {
                DisplayMessage(arg);
                //MessagingCenter.Unsubscribe<MainPageViewModel, NotificationDTO>(this, Constants.CHANGE_TEXT);
                //System.Diagnostics.Debug.WriteLine("test subscribe..."); // write many times when come back 
            });
        }
    
        public void SaveExecute()
        {
            MessagingCenter.Send(App.Locator.MainPageViewModel, Constants.CHANGE_TEXT,
                new NotificationDTO {
                    Title = Constants.INFO_TEXT,
                    Message = Constants.FORM_USER_TEXT,
                    Duration = TimeSpan.FromSeconds(2),
                    Type =  ToastNotificationType.Success });
    
            _navigationService.NavigateBack();
        }
    

    For some reason when pop the second view occurs this issue. Can anyone help me pls?

  • VinayakGawasVinayakGawas USMember ✭✭✭
    edited February 2017

    I had the same issue..
    I did a workaround by using boolean to limit the execution of method on geting message.
    You can do it like this.

        //Define a boolean.
         private bool _run = true;
                public bool run
                {
                    get { return _run; }
                    set { SetProperty(ref _run , value); }
                }
    
        //Your method
        void DisplayMessage(arg)
        {
         if (run)
                    {
                        //Code you want to run.
                    }
                    else
                    {
                        run = true;
                    }
        }
    

    Let me know if it worked or not.
    Regards
    Vinayak

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    If you take a look to docs

    https://developer.xamarin.com/guides/xamarin-forms/messaging-center/

    I think you should have a "send" like

    MessagingCenter.Send<MainPage> (this, "Hi");
    

    so, something like

    public void SaveExecute()
    {
        MessagingCenter.Send<MainPageViewModel, NotificationDTO>(this, Constants.CHANGE_TEXT,
            new NotificationDTO {
                Title = Constants.INFO_TEXT,
                Message = Constants.FORM_USER_TEXT,
                Duration = TimeSpan.FromSeconds(2),
                Type =  ToastNotificationType.Success });
    
        _navigationService.NavigateBack();
    }
    
  • geozedgeozed ARMember ✭✭
    Accepted Answer

    @AlessandroCaliaro i want to communicate two view models using message center because I'm implementing the MVVM pattern. So, the codebehind of the pages only set the binding context, nothing else.

    I've resolved it displaying the message in the page that will be popped. And I've removed the message center.

    @VinayakGawas I've tried to use a boolean property but in my app there are many viewmodels that sends messages. So, I 've decided to remove it.

    Thanks for your answers!

Sign In or Register to comment.