Alejandro
Alejandro

Reputation: 308

C# Xamarin Forms MessagingCenter not updating Item passed

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()
    {
        InitializeComponent();

        MessagingCenter.Subscribe<LoginPage, User>(this, "currentUserMainPage", (obj, item) =>
        {
            sUser = item;
            Debug.WriteLine("User updated from mainPage: " + sUser.firstName);
        });

        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)));
                    }
                    else
                    {
                        MenuPages.Add(id, new NavigationPage(new ProfilePage(sUser)));
                    }
                    break;
            }
        }
    }

Any idea what am I missing here?

Edit: here is the call from the LoginPage

protected override async void OnAppearing()
    {
        base.OnAppearing();

        try
        {
            //perform login
             MessagingCenter.Send(this, "currentUserMainPage", aUser.User);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        
    }

Upvotes: 3

Views: 935

Answers (1)

Junior Jiang
Junior Jiang

Reputation: 12723

First you can have a check with MessageCenter document : https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center

When subscribing and sending message , we need to keep the count and type of paramater be the same .

The example as follow :

Publish a Message : MessagingCenter.Send<MainPage, string>(this, "Hi", "John");

Subscribe to a message :

MessagingCenter.Subscribe<MainPage, string>(this, "Hi", async (sender, arg) =>
{
    await DisplayAlert("Message received", "arg=" + arg, "OK");
});

You will see that the first paramater is MainPage , and the second is string . They all need to set when publishing or subscribing .

In addition , using MessageCenter between different pages or classes , you can use object to replace or MainPage .

Therefore , shared code can be modified as follow :

Subscribing MessageCenter with object :

MessagingCenter.Subscribe<object, User>(this, "currentUserMainPage", (obj, item) =>
{
    sUser = item;
    Debug.WriteLine("User updated from mainPage: " + sUser.firstName);
});

And send message also with object :

MessagingCenter.Send<object,User>(this, "currentUserMainPage", aUser.User);

Upvotes: 2

Related Questions