Xamarin.Forms学习历程(五)——ListView简述

实在对不起大家,让大家等了这么久才等到这边文章。这段时间有点忙,学习压力有点大。我的Xamarin项目已经放弃,但是大家放心,我还是会把我这段时间所学的全部贴上去的,我也不想这样就抛弃了Xamarin,最起码C#还是比较重要的。这里我再暴露Xamarin的两个坑:

  • 1、Xamarin做一些简单功能是没有问题,最好不要涉及需要导入第三方库的功能,Android还比较容易,但是对iOS来说就不同了,需要将第三方库打包成framework,而且framework还要合并,这个教程网上很多,大家可以感受一下。可以看官网https://developer.xamarin.com/guides/cross-platform/macios/binding/ 百度&Google也是可以搜索到相关博客的。
  • 2、对Xamarin的生成的APP安装包没有要求的同学可以忽略。Xamarin在iOS上是将C#转成OC运行,而在Android上则是安装一个C#的编译器,以此来运行。我目测了一下,在iOS上应用程序只有3.5M左右,而在Android上能达到65M左右。看看我的github上工程有多大
Xamarin.Forms学习历程(五)——ListView简述 1
![Uploading Snip20160925_2_440677.png . . .]
Xamarin.Forms学习历程(五)——ListView简述 2
半天没下载下来.png

所以慎入!!!!!!!

Xamarin ListView###

在APP中用的最多的估计也就是表视图了,其他控件相对来说比较简单,我只说ListView,而且今天我只讲最复杂的ListView。其实Android的ListView与之十分相似。但是这里还有一个TableView,大家不要混淆,tableView的Cell需要手动添加,不会根据list自动生成。
我们今天就看自定义ListView。说道自定义ListView无非就是自定义ListView的cell。我们看代码,在代码里分析

方法一####

<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>

这里的ListView.ItemTemplate就是cell的自定义,照着格式写就可以了,至于里面Binding这个暂且不说,下一届我们将具体描述,ItemSelected = “customerListViewItemSelected”就是绑点CS文件里的点击事件。

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

这是CS文件里选中某一行的方法,是不是很简单,你或许会问,如果ListView要刷新数据怎么办?直接把原数据删掉再放入新数据就可以了。

customerListView.ItemsSource = null;
customerListView.ItemsSource = converted.Items;

方法二####

public class JFTradeCell : ViewCell
    {
        public JFTradeCell()
        {
            Label tradeInfoLab = new Label
            {
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.StartAndExpand,
                FontSize = 15,
            };
            tradeInfoLab.SetBinding(Label.TextProperty, "tradeDataInfo");

            Label tradeDateLab = new Label
            {
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.StartAndExpand,
                FontSize = 13,
                TextColor = Color.Gray,
            };
            tradeDateLab.SetBinding(Label.TextProperty, "tradeDate");

            Label perNetValueLab = new Label
            {
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.StartAndExpand,
                FontSize = 13,
                TextColor = Color.Gray,
            };
            perNetValueLab.SetBinding(Label.TextProperty, new Binding(
                                "unitPriceView",
                                stringFormat: "单位净值:{0}"));

            Label tradeAmountLab = new Label
            {
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.EndAndExpand,
                FontSize = 13,
                TextColor = Color.Gray,
            };
            tradeAmountLab.SetBinding(Label.TextProperty, new Binding(
                                "tradeAmount",
                                stringFormat: "总金额:{0}"));


            StackLayout tradeMoneyStackLayout = new StackLayout()
            {
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Orientation = StackOrientation.Horizontal,
                Children =
                {
                    perNetValueLab,
                    tradeAmountLab,
                }
            };

            StackLayout cellMainStackLayout = new StackLayout()
            {
                Padding = 5,
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                HeightRequest = 90,
                Orientation = StackOrientation.Vertical,
                Children =
                {
                    tradeInfoLab,
                    tradeMoneyStackLayout,
                    tradeDateLab,
                }
            };
            this.View = cellMainStackLayout;
        }
    }

用的时候这样用

tradeRecordList = new ListView
            {
                HasUnevenRows = true,
                ItemTemplate = new DataTemplate(typeof(JFTradeCell)),

            };
tradeRecordList.ItemSelected += tradeRecordListViewItemSelected;

我想到这里差不多会用了吧,看,你是不是会自定义View了,好吧,我把自定义View也交给你吧,在上述自定义cell的基础上将Viewcell换成contentView就可以了。

结语###

希望微软能将Xamarin完善起来,这样才有其发展之道。我也会持续更新我所学的内容,望大神指点。

User Review
0 (0 votes)
本站最新优惠

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

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

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

本文转载自Snoopy008,原文链接:https://www.jianshu.com/p/84a6b061838d,经授权后发布,本文观点不代表江湖人士立场,转载请联系原作者。

发表评论

登录后才能评论
RankMath 插件仅需$59,WordPress 最值得推荐的SEO插件,不限网站数量
见官网
直达官网