Home > database >  Why is the data from messagingcenter not being sent over
Why is the data from messagingcenter not being sent over

Time:11-22

Im new to C# and working in xamarin. i'm trying to load a user to be passed around the app to build a profile just for that user as thy use the app. I don't really care or optimization right now I just want to know why this isnt working. Code looks like this.

Loading user from login

private async void LoginButton_Clicked1(object sender, EventArgs e)
        {
            User user = new User();
            if (userNameEntry.Text == null || passwordNameEntry.Text == null)
            {
                await DisplayAlert("No info", "Please fill out UserName and Password", "Ok");
            }
            else
            {
                if (users != null)
                {
                    if (users.Exists(x => x.UserName.ToLower() == userNameEntry.Text.ToLower()))
                    {
                        user = users.Find(x => x.UserName.ToLower().Contains(userNameEntry.Text.ToLower()));

                        if (user.Password != passwordNameEntry.Text)
                        {
                            await DisplayAlert("Password MisMatch", "Password don't match. Try again!", "OK");
                            passwordNameEntry.Text = null;
                        }
                        else
                        {
                            
                            await Shell.Current.GoToAsync($"//{nameof(FeedPage)}");
                            MessagingCenter.Send(user,"CurrentUser");
                            userNameEntry.Text = null;
                            passwordNameEntry.Text = null;
                        }

trying to load that user somewhere else in app

public CoinPage()
        {
            InitializeComponent();
            this.add = new ToolbarItem
            {
                Text = "Add",
                Priority = 0,
                Order = ToolbarItemOrder.Primary
            };
            collectionView.SelectionChanged  = CollectionView_SelectionChanged;
            add.Command = new Command((sender) =>
            {
                this.AddCommand();
            });
         
            MessagingCenter.Subscribe<User>(this, "CurrentUser", (sender) =>
            {
                user = sender; - always null and can't figure out why
                
            });
        }

CodePudding user response:

first, you have to Subscribe before you Send a message

second, to pass an argument, use this syntax

MessagingCenter.Send<MyClass, User>(this, "CurrentUser", user);

where MyClass is the type sending the message, User is the type of the argument, this is a reference to the sender, CurrentUser is the message, and user is the argument

to subscribe

MessagingCenter.Subscribe<MyClass, User>(this, "CurrentUser", async (sender, arg) =>
{
  // do stuff
});

note that the types <T1,T2> must match between Send and Subscribe

CodePudding user response:

For your problem, you can refer to the following code:

1.Subscribe to a message

        MessagingCenter.Subscribe<object, object>(this, "CurrentUser", (sender, args) =>
        {
            User model = (User)args;

        });

2.Publish a message

        User user = new User { Name = "user1" ,age = 18};

        MessagingCenter.Send<object, object>(this, "CurrentUser", user);

Note:

Make sure you have subscribe to a message before publishing a message.

For more, check:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center

  • Related