简体   繁体   English

C# Xamarin Forms 消息中心未更新项目已通过

[英]C# Xamarin Forms MessagingCenter not updating Item passed

I am using MessagingCenter to passe objects throu my pages, from my LoginPage to my MainPage.我正在使用MessagingCenter通过我的页面传递对象,从我的 LoginPage 到我的 MainPage。 Even tho the object is updated, when using it to my mainpage, the object seems to be null.即使object已更新,在我的主页上使用它时,object 似乎是 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.当我在更改页面之前检查 object 时,即使它不再是 null,它也会返回给我 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编辑:这是来自 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);
        }
        
    }

First you can have a check with MessageCenter document: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center首先,您可以查看 MessageCenter 文档: https://docs.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");发布消息: 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 .你会看到第一个参数是MainPage ,第二个是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 .另外,在不同页面或类之间使用 MessageCenter,可以使用objectMainPage来代替。

Therefore , shared code can be modified as follow:因此,共享代码可以修改如下:

Subscribing MessageCenter with object :使用object订阅MessageCenter

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

And send message also with object :并使用object发送消息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM