【问题标题】:ListView not triggering ItemTappedListView 没有触发 ItemTapped
【发布时间】:2018-10-23 23:42:11
【问题描述】:

我一直在做一项学校作业,您需要将数据加载到 ListView 中。根据课程手册,您需要使用我想出的“ObservableCollection”。但是,我似乎无法让 ItemTapped 工作。

我正在使用 SQLite 创建下表。

public class Settings
{
    [Primarykey]
    public string Name { get; set; }

    [MaxLength(255)]
    public string Value { get; set; }
}

然后在 OnAppearing 上初始化数据库并添加一行数据。

public partial class SQL : ContentPage
{
    private SQLiteAsyncConnection _connection;
    private ObservableCollection<Settings> _settings;

    public SQL()
    {
        InitializeComponent();


        _connection = DependencyService.Get<ISQLiteDb>().GetConnection();
    }

    protected override async void OnAppearing()
    {
        await _connection.CreateTableAsync<Settings>();

        var settings_name = new Settings { Name = "Meaning of Life", Value = "42" };
        await _connection.InsertAsync(settings_name);

        await DisplayAlert("Alert", "Value Added to database!", "OK");


        var settings = await _connection.Table<Settings>().ToListAsync();
        _settings = new ObservableCollection<Settings>(settings);
        ListView.ItemsSource = _settings;

        base.OnAppearing();
    }

    void MyItemTapped (object sender, System.EventArgs e)
    {
        DisplayAlert("Alert", "You Pressed Something!", "OK");
    }

    void MyItemSelected (object sender, System.EventArgs e)
    {
        DisplayAlert("Alert", "You Selected Something!", "OK");
    }
}

在我的 XAML 文件中,我有以下内容,ItemTapped 转到上面的函数。

<?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="BudgetBuddy.SQL">
    <ContentPage.Content>
                <ListView x:Name="ListView">
            <ListView.ItemTemplate ItemTapped="MyItemTapped" ItemSelected="MyItemSelected">
                <DataTemplate>
                    <TextCell Text="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

我不知道我做错了什么。为什么我的 ItemTapped 和 ItemSelected 不起作用?此外,从 ObservableCollection 访问与我在 ListView 中按下的名称关联的值的最佳方法是什么

【问题讨论】:

    标签: xaml xamarin xamarin.forms xamarin.ios


    【解决方案1】:

    您的 ItemTapped 和 ItemSelected 不起作用的原因是它们位于错误的位置。

    这就是你所拥有的:

    <?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="BudgetBuddy.SQL">
        <ContentPage.Content>
                    <ListView x:Name="ListView">
                <ListView.ItemTemplate ItemTapped="MyItemTapped" ItemSelected="MyItemSelected">
                    <DataTemplate>
                        <TextCell Text="{Binding Name}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ContentPage.Content>
    </ContentPage>
    

    这是你应该做的:

    <?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="BudgetBuddy.SQL">
        <ContentPage.Content>
                    <ListView x:Name="ListView" ItemTapped="MyItemTapped" ItemSelected="MyItemSelected">
                <ListView.ItemTemplate >
                    <DataTemplate>
                        <TextCell Text="{Binding Name}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ContentPage.Content>
    </ContentPage>
    

    【讨论】:

    • 这似乎可行,你知道我如何访问与按下的文本关联的值吗?
    • @JohnCadac 将 e.Item 转换为对象的类型,以便您可以访问它的属性。
    猜你喜欢
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    相关资源
    最近更新 更多