【问题标题】:XAML ListView in Xamarin does not workXamarin 中的 XAML ListView 不起作用
【发布时间】:2017-11-19 16:57:38
【问题描述】:

我开始学习 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; } 
}

【问题讨论】:

  • 尝试从您的 DataTemplate 中删除按钮

标签: c# sqlite xaml listview xamarin


【解决方案1】:

尝试将TextCellButton 放在StackLayout&lt;ViewCell&gt; 元素内的任何布局中:

<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

【讨论】:

    【解决方案2】:

    您不能将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>
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-05-08
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 2016-10-31
      • 1970-01-01
      • 2021-07-21
      • 2021-12-03
      相关资源
      最近更新 更多