Syncfusion.ListView 绑定到可观察集合(Xamarin)

问题描述 投票:0回答:1

我正在努力(尽管我已经查阅了您的文档)来了解如何将 Syncfusion SfListView 控件绑定到 Observable Collection。

我有一个非常简单的 Xamarin Mobile 应用程序..

我有一个视图(链接到模型)从 WebAPI 拉回数据。 数据检索一切正常 - 但是我不知道如何将 ListView 控件绑定到 OPractices ObservableCollection..

我不需要视图模型,因此我尝试绑定到 ObservableCollection...

请帮忙 - 我缺少什么

XAML:

<?xml version="1.0" encoding="utf-8" ?>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"

             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

             xmlns:nscMobileApp="clr-namespace:NSC_MobileApp;assembly=NSC_MobileApp"

             xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"

             x:Class="NSC_MobileApp.Views.PracticeList">

    <syncfusion:SfListView x:Name="ListOfPractices" ItemsSource="{Binding Practices}">



    <syncfusion:SfListView.ItemTemplate>

        <DataTemplate>

            <Grid Padding="10">

                <Grid.RowDefinitions>

                    <RowDefinition Height="0.4*" />

                    <RowDefinition Height="0.6*" />

                </Grid.RowDefinitions>

                <Label Text="{Binding Name}" FontAttributes="Bold" TextColor="Teal" FontSize="21" />

                <Label Grid.Row="1" Text="{Binding Address}" TextColor="Teal" FontSize="15"/>

            </Grid>

        </DataTemplate>

    </syncfusion:SfListView.ItemTemplate>



    </syncfusion:SfListView>



</ContentPage>

我的模特:

namespace NSC_MobileApp

{

    [Table("Practices")] //Table Name

    public class Practices

    {

        public int ID { get; set; }

        public string Name { get; set; }

        public string Address { get; set; }

        public string ContactName { get; set; }

        public string ContactAddress { get; set; }

        public string ContactEmail { get; set; }



        public int? Milage { get; set; }

    }

}

我的看法:

using Newtonsoft.Json;

using Syncfusion.ListView.XForms;

using Syncfusion.SfCalendar.XForms;

using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.Linq;

using System.Net.Http;

using System.Text;

using System.Threading.Tasks;



using Xamarin.Forms;

using Xamarin.Forms.Xaml;



namespace NSC_MobileApp.Views

{

    [XamlCompilation(XamlCompilationOptions.Compile)]

    public partial class PracticeList : ContentPage

    {



        private const string URL = "https://XXXXXX/Practices";

        private HttpClient _client = new HttpClient();

        public ObservableCollection<Practices> Opractices;





        public PracticeList()

        {

            InitializeComponent();



        }



        protected override async void OnAppearing()

        {

            var content = await _client.GetStringAsync(URL);

            var posts = JsonConvert.DeserializeObject<List<Practices>>(content);



            Opractices = new ObservableCollection<Practices>(posts);



            //-------------------------------------------------------------------------------------------



        }
xamarin syncfusion
1个回答
0
投票

根据我最后的评论:

找到答案: XAML:

<syncfusion:SfListView.ItemTemplate> 
<DataTemplate> 
<!-- Your item template here --> 
<Label Text="{Binding Name}" /> </DataTemplate> 
</syncfusion:SfListView.ItemTemplate> 

背后代码:

Opractices = new ObservableCollection<Practices>(posts); ListOfPractices.ItemsSource = Opractices; 
© www.soinside.com 2019 - 2024. All rights reserved.