fetch data from server and send back to page using MessageCenter

时间:2017-04-06 17:00:14

标签: xamarin

I am trying to get data from server and sending back to the xamarin form page using MessageCenter

var content = await response.Content.ReadAsStringAsync();
Response responseData = JsonConvert.DeserializeObject<Response>(content);
if(responseData!=null)
   MessagingCenter.Send<RestApi, Response>(this, GET_DATA, responseData);
else
   MessagingCenter.Send<RestApi, Response>(this, GET_DATA, responseData);

and in MainPage i have subscribed in as follows

  public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();


            MessagingCenter.Subscribe<MainPage, Response>(this, RestApi.GET_DATA, (sender, arg) => {

                Debug.WriteLine("Count of data {0}", arg.data.Count);

            });


            RestApi restApi = new RestApi();
            restApi.getData();

        }


    }

But my control never reaches to Debug.WriteLine("Count of data {0}", arg.data.Count);

1 个答案:

答案 0 :(得分:1)

Your TSender parameter do not match between the Subscribe and Publish and thus the message is never received by your "subscribers"

Something like this would work:

MessagingCenter.Subscribe<RestApi, Response>(this, RestApi.GET_DATA, (sender, arg) => {
    Debug.WriteLine("Count of data {0}", arg.data.Count);
});
MessagingCenter.Send<RestApi, Response>(this, GET_DATA, responseData);