Binding properties to a ListView in Xamarin, does not update the View

Viewed 384

I am having trouble, with binding properties to a ListView in Xamarin Forms. What I am trying to do is:

        {
            InitializeComponent();
            //Task.Run(async () => await GetTaskDetails());


            GetTasks();

        }

        ObservableCollection<XMCTasks> _userTasks;

        private async void GetTasks()
        {
            using (var client = new HttpClient())
            {
                string userId = "";
                if(Application.Current.Properties["userId"] != null)
                {
                    userId = Application.Current.Properties["userId"].ToString();
                }
                var uri = "http://diplomaxmcws-dev.us-east-2.elasticbeanstalk.com/api/Tasks/GetUserTasks?userId=" + userId;
                var result = await client.GetStringAsync(uri);
                var TaskList = JsonConvert.DeserializeObject<List<XMCTasks>>(result);
                UserTasks = new ObservableCollection<XMCTasks>(TaskList);
            }
        }

        public ObservableCollection<XMCTasks> UserTasks
        {
            get
            {
                return _userTasks;
            }
            set
            {
                _userTasks = value;
                OnPropertyChanged();
            }
        }

Getting a list of objects from a Rest API call, and after that binding those objects to a ListView:

<ListView ItemsSource="{Binding UserTasks}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.View>
                                <StackLayout>
                                   <!-- <Label Text="{Binding XMCTask_Id, StringFormat='XMCTask_Id: {0:F0)}'}"></Label> -->
                                    <Label Text="{Binding Task_Name, StringFormat='Task_Name: {0:F0)}'}" TextColor="Black" FontSize="Medium"></Label>
                                    <!-- <Label Text="{Binding Task_Description, StringFormat='Task_Description: {0:F0)}'}"></Label>
                                    <Label Text="{Binding Creator_Id, StringFormat='Creator_Id: {0:F0)}'}"></Label> 
                                     <Label Text="{Binding Referencer_Id, StringFormat='Referencer_Id: {0:F0)}'}"></Label>
                                    <Label Text="{Binding XMCPune_Id, StringFormat='XMCPune_Id: {0:F0)}'}"></Label>
                                    <Label Text="{Binding XMCProjekt_Id, StringFormat='XMCProjekt_Id: {0:F0)}'}"></Label>
                                    <Button Text="{Binding XMCTipologjia_Id, StringFormat='XMCTipologjia_Id: {0:F0)}'}"></Button>-->
                                </StackLayout>
                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

I don't know if it has to do with the lifecycle of a Xamarin Forms Page. Just to let you know, this page gets loaded after a successful login. Since the process of getting the results from the web service is async it may be possible something to get messed up. Any approach is much appreciated.

1 Answers

The BindingContext property of the target object must be set to the source object.

If you want to use ItemsSource="{Binding UserTasks} in the xaml,you should add BindingContext = this;in your page.cs constructor as Jason said above.

or you could define x:Name for your ListView in xaml,then set ItemSource in your page.cs:

<ListView x:Name="listview">
  ...
</ListView>


private async void GetTasks()
    {
        using (var client = new HttpClient())
        {
            string userId = "";
            if(Application.Current.Properties["userId"] != null)
            {
                userId = Application.Current.Properties["userId"].ToString();
            }
            var uri = "http://diplomaxmcws-dev.us-east-2.elasticbeanstalk.com/api/Tasks/GetUserTasks?userId=" + userId;
            var result = await client.GetStringAsync(uri);
            var TaskList = JsonConvert.DeserializeObject<List<XMCTasks>>(result);
            UserTasks = new ObservableCollection<XMCTasks>(TaskList);
            listview.ItemsSource = UserTasks;
        }
    }
Related