簡體   English   中英

Xamarin中的XAML ListView不起作用

[英]XAML ListView in Xamarin does not work

我開始學習Xamarin,並且試圖創建第一個測試項目。 我創建了ListView,它顯示了BindingContext中的ObservableCollection。 “ Take”是具有三個屬性的表。 問題是,當我在模擬器上運行應用程序時,出現以下錯誤對話框:“進程系統無響應。您要關閉它嗎?”
但是,如果我刪除標簽和所有內部XAML代碼,則一切正常,但是我想在ListView的每個元素中分配“ Take”類的屬性值。

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

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="App1.Views.Page1">

<ContentPage.Content>
    <StackLayout>
        <Label Text="Welcome to my first project"></Label>
        <Label Text="Why does it does not work?"></Label>
        <ListView ItemsSource="{Binding GetList}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding Word}"></TextCell>
                    <Button Text="SomeText"></Button>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>
</ContentPage>

這是我的C#BindingContext

public class SQLiteSamplePage
{
    private readonly SQLiteConnection _sqLiteConnection;
    private ObservableCollection<Take> list;

    public SQLiteSamplePage()
    {
        _sqLiteConnection = DependencyService.Get<ISQLite>().GetConnection();
        _sqLiteConnection.CreateTable<Take>();
        _sqLiteConnection.Insert(new Take
        {
            Word = "SomeWord1",
            Translation = "Some translation 1"
        });

        _sqLiteConnection.Insert(new Take
        {
            Word = "SomeWord",
            Translation = "SomeTranslation"
        });

        list =new ObservableCollection<Take>( _sqLiteConnection.Table<Take>());
    }
    public ObservableCollection<Take> GetList
    {
        get { return list; }
    }
}

這是表格的代碼

public class Table
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; } 
    public string Word { get; set; } 
    public string Translation { get; set; } 
}

嘗試將TextCellButton放在StackLayout<ViewCell>元素內的任何布局中:

<ListView ItemsSource="{Binding GetList}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <TextCell Text="{Binding Word}"></TextCell>
                    <Button Text="SomeText"></Button>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

DataTemplate元素的子級必須是ViewCell類型或從ViewCell類型ViewCell

您不能將TextCell和其他東西(如果是Button結合在一起。 如果要同時顯示“文本”和“按鈕”,則需要使用自定義單元格。

使用基類ViewCell完成此操作,並在其中定義要顯示的布局

<StackLayout>
    <Label Text="Welcome to my first project"></Label>
    <Label Text="Why does it does not work?"></Label>
    <ListView ItemsSource="{Binding GetList}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label Text="{Binding Word}"></Label>
                        <Button Text="SomeText"></Button>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM