自定義單元格

在cs代碼中完成

自定義單元格

public class EmployeeCell : ViewCell
{
    public EmployeeCell()
    {
        var image = new Image
        {
            HorizontalOptions = LayoutOptions.Start
        };
        image.SetBinding(Image.SourceProperty, new Binding("ImageUri"));
        //設(shè)置寬高為40
        image.WidthRequest = image.HeightRequest = 40;

        var nameLayout = CreateNameLayout();
        var viewLayout = new StackLayout()
        {
            Orientation = StackOrientation.Horizontal,
            //加入圖片、名稱、推特
            Children = { image, nameLayout }
        };
        //把布局賦值給View
        View = viewLayout;
    }

    static StackLayout CreateNameLayout()
    {
        //新增Label
        var nameLabel = new Label
        {
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        //綁定Employee的DisplayName屬性
        nameLabel.SetBinding(Label.TextProperty, "DisplayName");

        var twitterLabel = new Label
        {
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        twitterLabel.SetBinding(Label.TextProperty, "Twitter");

        var nameLayout = new StackLayout()
        {
            HorizontalOptions = LayoutOptions.StartAndExpand,
            //設(shè)置為從上到下排列
            Orientation = StackOrientation.Vertical,
            //將兩個(gè)Label依次添加到Children中
            Children = { nameLabel, twitterLabel }
        };
        return nameLayout;
    }
}

后臺(tái)

List<Employee> EmployeeList = new List<Employee>();
EmployeeList.Add(new Employee()
{
    DisplayName = "Jack",
    Twitter ="@fake4",
    ImageUri= "http://v1.qzone.cc/avatar/201406/24/21/03/53a977066f053731.jpg!200x200.jpg"
});
EmployeeList.Add(new Employee()
{
    DisplayName = "Tom",
    Twitter = "@mml1",
    ImageUri= "http://diy.qqjay.com/u2/2014/0628/da687c0fb5b3bb8cd31dec7d8865aea8.jpg"
});
EmployeeList.Add(new Employee()
{
    DisplayName = "Tony",
    Twitter = "@wood564",
    ImageUri= "http://v1.qzone.cc/avatar/201406/24/21/03/53a977066f053731.jpg!200x200.jpg"
});
var listView = new ListView
{
    RowHeight = 80
};
listView.ItemsSource = EmployeeList;
//注意:此時(shí)指定模板為寫好的EmployeeCell
listView.ItemTemplate = new DataTemplate(typeof(EmployeeCell));
Content = new StackLayout
{
    VerticalOptions = LayoutOptions.FillAndExpand,
    Children = { listView }
};

Employee類

public class Employee
{
    public string DisplayName { get; set; }
    public string Twitter { get; set; }
    public string ImageUri { get; set; }
}

效果

1

在xaml中完成

后臺(tái)賦值

List<Employee> Employees = new List<Employee>();
//添加數(shù)據(jù)
BindingContext = Employees;

前端綁定對(duì)應(yīng)屬性名
BindingContext已經(jīng)賦值,ItemsSource直接綁定下來(lái)即可

<ListView x:Name="listView" ItemsSource="{Binding }">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout Orientation="Horizontal">
                        <Image Source="{Binding ImageUri}" WidthRequest="40" HeightRequest="40" />
                        <StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand">
                            <Label Text="{Binding DisplayName}" HorizontalOptions="FillAndExpand" />
                            <Label Text="{Binding Twitter}"/>
                        </StackLayout>
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

注意:Employee實(shí)不實(shí)現(xiàn)INotifyPropertyChanged接口,均可以展示,只是看你做不做雙向綁定

示例代碼

https://github.com/zLulus/NotePractice/tree/dev3/Xamarin.Forms/XamarinDemo/XamarinDemo/XamarinDemo/CustomizingCell

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 記錄一個(gè)菜鳥的iOS學(xué)習(xí)之旅,如能幫助正在學(xué)習(xí)的你,亦楓不勝榮幸;如路過(guò)的大神如指教幾句,亦楓感激涕淋! iOS系...
    亦楓閱讀 1,949評(píng)論 4 9
  • 記錄一個(gè)菜鳥的iOS學(xué)習(xí)之旅,如能幫助正在學(xué)習(xí)的你,亦楓不勝榮幸;如路過(guò)的大神如指教幾句,亦楓感激涕淋! 在亦楓的...
    亦楓閱讀 1,607評(píng)論 2 5
  • 記錄一個(gè)菜鳥的iOS學(xué)習(xí)之旅,如能幫助正在學(xué)習(xí)的你,亦楓不勝榮幸;如路過(guò)的大神如指教幾句,亦楓感激涕淋! 在前面兩...
    亦楓閱讀 1,606評(píng)論 0 5
  • 1、你差哪里都差沒得救 剛上一年級(jí)的小明期末考試全班倒數(shù)第一,小明媽媽爸爸坐在小明左右,一個(gè)拿著考試卷問(wèn),你給媽媽...
    雅米飯閱讀 234評(píng)論 0 0
  • 處暑侵晨,時(shí)雷霆大作,暴雨將至。路師弟燥熱難耐輾轉(zhuǎn)未眠。在床側(cè)反復(fù)滾動(dòng),楚師兄幾欲入眠奈何動(dòng)靜甚大,孰不可忍,抓過(guò)...
    沉肅閱讀 288評(píng)論 0 1

友情鏈接更多精彩內(nèi)容