C# Xamarin Forms MessagingCenter not updating Item passed RRS feed

  • Question

  • User388390 posted

    I am using MessagingCenter to passe objects throu my pages, from my LoginPage to my MainPage. Even tho the object is updated, when using it to my mainpage, the object seems to be null.

    public User sUser { get; set; }
    public MainPage()
        MessagingCenter.Subscribe<LoginPage, User>(this, "currentUserMainPage", (obj, item) =>
            sUser = item;
            Debug.WriteLine("User updated from mainPage ");
        MasterBehavior = MasterBehavior.Popover;
        MenuPages.Add((int)MenuItemType.Home, (NavigationPage)Detail);

    When I check for the object before changing pages, even tho it is not null anymore, it returns me null.

    public async Task NavigateFromMenu(int id)
        if (!MenuPages.ContainsKey(id))
            switch (id)
                case (int)MenuItemType.Profile:
                    if(sUser == null)
                        MenuPages.Add(id, new NavigationPage(new LoginPage(sUser)));
                        MenuPages.Add(id, new NavigationPage(new ProfilePage(sUser)));
    Tuesday, June 30, 2020 10:44 PM

All replies

  • User39110 posted

    Dear @iAndi First step you have to send MessagingCenter.Send<>();

    from login page
    MessagingCenter.Send<MainPage, string>(this, "Hi", "John");
    second step receive 
    on main page
    MessagingCenter.Subscribe<MainPage, string>(this, "Hi", async (sender, arg) =>
        await DisplayAlert("Message received", "arg=" + arg, "OK");
    Wednesday, July 1, 2020 7:27 AM
  • User369979 posted

    Did this Subscribe event trigger when you send messaging center on the login page? Could you please share the code of the send method? You could register this like:

    MessagingCenter.Subscribe<object, object>(this, "currentUserMainPage", (sender, item) =>

    This sort of messaging center could receive any object on any page like:

    MessagingCenter.Send<object, object>(this, "currentUserMainPage", user);
    Wednesday, July 1, 2020 7:50 AM