0

I'm developing a sample where Messaging Center send status messeges not coupled from device code to my view models. At this point i used a alert message to notice the event before try in View models.

For it I used a static view instance in my share application constructor (App.xaml) where in view constructor I Subscript the status.

App (shared)

public partial class App : Application
    {

        #region MasterDetailPage
        public static MasterDetailPage MDP;
        public static NavigationPage NAV = null;
        public static MainView _mainpage;
        #endregion

        public App ()
        {
            InitializeComponent();
            NAV = new NavigationPage(new StarterView()) { BarBackgroundColor = Color.FromHex("701424"), BarTextColor = Color.White }; ;
            MDP = new MasterDetailPage();
            MDP.BackgroundColor = Xamarin.Forms.Color.FromHex("701424");
            _mainpage = new MainView();
            MDP.Master = _mainpage;
            MDP.Detail = NAV;
            MainPage = MDP;
            MainPage.Title = "H2X";


        }

(View shared)

public MainView ()
        {
            InitializeComponent ();

            string a="Test";
            #region MessegeCenter

            MessagingCenter.Subscribe<string,string>("APP", "Message_Received", async (sender,arg) => 
            {
                string b = a;
                a = $"{arg}";
                await DisplayAlert("Atenção", a+b, "Ok");
            });
            #endregion

        }

Into the specific platform code (Device - UWP) I create a timer that sends messages after some time instanced in mainpage constructor.

void dispatcherTimer_Tick(object sender, object e)
        {
            DateTimeOffset time = DateTimeOffset.Now;
            TimeSpan span = time - lastTime;
            lastTime = time;
            //Time since last tick should be very very close to Interval
            TimerLog.Text += timesTicked + "\t time since last tick: " + span.ToString() + "\n";
            timesTicked++;
            if (timesTicked > timesToTick)
            {                   
                MessagingCenter.Send<string,string>("APP","Message_Received","MR");
            }
        }

When I run it, twice alert messages with the same text are opening, but there aren't two subscriptions. The same text give me the information that it was from the same send event.

Where is the problem ? Is there any relationship with my static view ?

Thank you in advance

Guilherme

  • 1
    `MessagingCenter.Subscribe` is probably called twice. Try to debug your code and find out why it's called twice. – Dennis Schröer Oct 24 '18 at 07:45
  • Does it only appear in UWP platform? – Nico Zhu Oct 24 '18 at 08:00
  • 1
    Sir, I don't know why but I delete my bin and obj folders insted of clean the solution. When I ran it after close and open again the MessagingCenter works. Probably it's one random bug that didn't clean correctly the folders. By the way, thx Dennis Schoroer and Nico Zhu to try to help me:) Regards for all – Guilherme Marques Oct 24 '18 at 12:40

1 Answers1

1

It is a good practice to always unsubscribe from the MessagingCenter.

MessagingCenter.Unsubscribe<string, string>(this, "Message_Received");

If MessagingCenter is subscribed twice ,then functions will be call twice.

bhavya joshi
  • 1,096
  • 10
  • 20