Xamarin.Forms学习历程(六)——数据绑定

久等了,今天讲Xamarin.Forms里的核心——数据绑定,是将数据绑定到对应的view上,实现cs文件与XML文件的数据流通。今天普通的数据绑定我不做讲解,今天只讲个最复杂的,就是将接口解析的数据为模型数组,再将模型数组映射到对应的ListView上。下面就来逐一讲解。

1、新建数据模型##

根据接口的数据返回值新建对应的模型。

    public class JFCustomers
    {
        [JsonProperty("list")]
        public IEnumerable<JFCustomer> Items { get; set; }
    }

    public class JFCustomer
    {
        [JsonProperty("address")]
        public string address { get; set; }

        [JsonProperty("customerType")]
        public string customerType { get; set; }

        [JsonProperty("customerTypeView")]
        public string customerTypeView { get; set; }

        [JsonProperty("id")]
        public string id { get; set; }

        [JsonProperty("investProductCount")]
        public string investProductCount { get; set; }

        [JsonProperty("investShareAmountTotal")]
        public string investShareAmountTotal { get; set; }

        [JsonProperty("investShareTotal")]
        public string investShareTotal { get; set; }

        [JsonProperty("mobile")]
        public string mobile { get; set; }

        [JsonProperty("name")]
        public string name { get; set; }

    }
[JsonProperty(“XXXX”)]是为了解析数据能映射到模型对应的属性上去,所以尽量让模型属性名与Json里数据值对应的“key”名字保持一致。

2、调接口,获取数据##

这里网络使用的是httpclient,请不要使用request,因为request里有些方法已经不再使用。

    protected override async void OnAppearing()
    {
        base.OnAppearing();
        int currentPage = 1;
        String sequenceName = "investShareAmountTotal";
        String uri = JFGobalData.baseUrl + "/api/fund/clients?keyWords=" + searchBar.Text + "&page=" + currentPage + "&pageSize=10&sort=" + sequenceName;
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("X-Auth-Token",urlToken);
        var task = await client.GetAsync(uri);
        var jsonString = await task.Content.ReadAsStringAsync();
        var converted = JsonConvert.DeserializeObject<JFCustomers>(jsonString);
        customerListView.ItemsSource = converted.Items;
    }

这里是在界面刚开始加载时调用接口,我们只需要看最后两行代码

var converted = JsonConvert.DeserializeObject<JFCustomers>(jsonString);
customerListView.ItemsSource = converted.Items;

第一行是将json解析成模型数组,第二行是将模型数组给ListView的ItemsSource,这样我们就完成了宏观的数据绑定,我们还需要将模型里的各个数据放到对应的view里展示。

3、模型属性分发给View##

这里才是最后的重中之重,我们还是看代码来分析

<ListView x:Name = "customerListView"
                    SeparatorVisibility = "None"
                        ItemSelected = "customerListViewItemSelected">
                <ListView.RowHeight>
                     <OnPlatform x:TypeArguments="x:Int32"
                                iOS="80"
                                Android="80"
                                WinPhone="90" />
                </ListView.RowHeight>

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ContentView>
                                <Frame OutlineColor = "Accent"
                                        Padding = "5">
                                    <StackLayout Orientation = "Vertical">
                                            <Label Text = "{Binding name}"
                                                FontSize = "18"
                                                TextColor = "Black"
                                                HorizontalOptions = "StartAndExpand"/>

                                        <StackLayout Orientation = "Horizontal">
                                            <Label Text = "客户类别"
                                                FontSize = "12"
                                                TextColor = "Gray"
                                                HorizontalOptions = "StartAndExpand"/>

                                            <Label Text = "{Binding customerType}"
                                                FontSize = "12"
                                                TextColor = "Black"
                                                HorizontalOptions = "StartAndExpand"/>

                                            <Label Text = "投资产品(个)"
                                                FontSize = "12"
                                                TextColor = "Gray"
                                                HorizontalOptions = "StartAndExpand"/>
                                            <Label Text = "{Binding investProductCount}"
                                                FontSize = "12"
                                                TextColor = "Black"
                                                HorizontalOptions = "StartAndExpand"/>
                                        </StackLayout>

                                        <StackLayout Orientation = "Horizontal">
                                            <Label Text = "投资份额(份)"
                                                FontSize = "12"
                                                TextColor = "Gray"
                                                HorizontalOptions = "StartAndExpand"/>

                                            <Label Text = "{Binding investShareTotal}"
                                                FontSize = "12"
                                                TextColor = "Black"
                                                HorizontalOptions = "StartAndExpand"/>

                                            <Label Text = "投资金额(元)"
                                                FontSize = "12"
                                                TextColor = "Gray"
                                                HorizontalOptions = "StartAndExpand"/>
                                            <Label Text = "{Binding investShareAmountTotal}"
                                                FontSize = "12"
                                                TextColor = "Black"
                                                HorizontalOptions = "StartAndExpand"/>
                                        </StackLayout>
                                    </StackLayout>
                                </Frame>
                            </ContentView>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

其实这里真没有什么可说而
我只说一点

ItemSelected = "customerListViewItemSelected"

这个用来给每一行元素提供点击事件,或许你会问,我怎么知道我点击的是哪一行,下面就给你解答。

4、获取点击的模型##

void customerListViewItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    JFCustomerDetailPage customerDetailPage = new JFCustomerDetailPage();
    customerDetailPage.customerModel = (JFCustomer)e.SelectedItem;
    this.Navigation.PushAsync(customerDetailPage);
}

这样你就可以拿到你所点击那一行。

结语#

本人才疏学浅,如有描述不对的地方望大神指正。还有Xamarin目前似乎不在维护,请小心跳坑,个人认为拿来做小应用是可以的,大型应用最好还是寻找别的解决方案。谢谢

猜你喜欢

本站最新优惠

Namesilo优惠:新用户省 $1 域名注册-优惠码:45D%UYTcxYuCloZ 国外最便宜域名!点击了解更多

特别优惠:免费赠送 $100 Vultr主机-限时优惠!英文站必备海外服务器!点击了解更多

VPS优惠:搬瓦工优惠码:BWH3OGRI2BMW 最高省5.83%打开外面世界的一款主机点击了解更多

本文转载自Snoopy008,原文链接:https://www.jianshu.com/p/5229274f5391,本文观点不代表江湖人士立场,转载请联系原作者。

(0)
江小编的头像江小编
上一篇 2018-01-26 09:58
下一篇 2018-01-26

热门推荐

发表回复

登录后才能评论
畅访海外网站,外贸/外企/科技工作者专用工具,无缝体验真实的互联网,解锁LinkedIn访问
$19.95 /年
直达官网