Xamarin C#“结合:在'LastSince.TasksClass'上找不到'taskName'属性,目的属性:“Xamarin.Forms.Label.Text'"设置列表视图[重复]

nkcskrwz  于 2022-12-07  发布在  C#
关注(0)|答案(1)|浏览(117)

This question already has an answer here:

Xamarin Binding property not found (1 answer)
Closed 20 days ago.
I am trying to set my items to appear on a ListView but when I run the app the listview displays "Name" with the number of objects contained in the JSON file but it displays in the console log
"Binding: 'taskName' property not found on 'LastSince.TasksClass', target property: 'Xamarin.Forms.Label.Text'" I am not sure why but my class and listview setup are below:

<ListView BackgroundColor="#686d7e"
                      Margin="0,0,0,0"
                      HeightRequest="350"
                      x:Name="sinceItemsListView">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout>
                                    <Label Text="Name"></Label>
                                    <Label Text="{Binding taskName}"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

And the C# class is:

public string taskName;
        public string taskDescription;
        public string taskPriority;

        public TasksClass(string taskName, string taskDescription, string taskPriority)
        {
            this.taskName = taskName;
            this.taskDescription = taskDescription;
            this.taskPriority = taskPriority;
        }

and finally, the code to apply the listview is:

ObservableCollection<TasksClass> tasksList { get; set; }
        

        fileHandler fileHandler = new();
        public string loadedJson;

        public Tasks()
        {
            InitializeComponent();
            tasksList = new ObservableCollection<TasksClass>();
            sinceItemsListView.ItemsSource = tasksList;
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            DeseraliseJSON();
            sinceItemsListView.ItemsSource = tasksList;
        }

I am not sure why this is wrong; I can only assume the binding isn't setup correctly but appreciate any help.
I tried running a listview of items but returns empty values because the binding property cannot be found

rfbsl7qr

rfbsl7qr1#

请尝试替换视图模型中的属性taskName,如下所示:

public string taskName;

public string taskName { get; set; }

相关问题