【问题标题】:How to add ItemTapped on ListView with аutomatic assignment of ID from SQLite using xamarin如何使用 xamarin 从 SQLite 自动分配 ID 在 ListView 上添加 ItemTapped
【发布时间】:2021-08-05 05:32:52
【问题描述】:

我有一个输入 ID 的字段和一个行读取按钮。 当您输入 ID 并单击按钮时,会出现一条消息,其中包含相应行上的信息。 我想删除“读取”按钮和输入字段,以便当用户单击相应的行时,将显示一条包含该行信息的消息?

我的 xaml.cs 代码是:

async void ReadCity()
    {
        if (!string.IsNullOrEmpty(txtPersonId.Text))
        {
            //Get Person
            var person = await App.SQLiteDb.GetItemAsync(Convert.ToInt32(txtPersonId.Text));
            if (person != null)
            {
               
                await DisplayAlert("Success", "Person Name: " + person.Name, "OK");
            }
        }
        else
        {
            await DisplayAlert("Required", "Please Enter PersonID", "OK");
        }
    }

    void BtnRead_Clicked(System.Object sender, System.EventArgs e)
    {
        ReadCity();
    }

我的 xaml 代码是:

            <StackLayout HorizontalOptions="Center" VerticalOptions="Start">
            <Label Margin="0,0,0,10" Text="FAVORITES" FontAttributes="Bold" FontSize="Large" TextColor="Gray" HorizontalTextAlignment="Center" ></Label>
            <Entry x:Name="txtPersonId" Placeholder="City ID for Delete"></Entry>
            <StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
            <Button x:Name="btnDelete" WidthRequest="200" Text="Delete" Clicked="BtnDelete_Clicked" />
                <Button x:Name="btnRead" WidthRequest="200" Text="Read" Clicked="BtnRead_Clicked" />
            </StackLayout>
            <ListView x:Name="lstPersons">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding Name}" Detail="{Binding PersonID}"></TextCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            </StackLayout>

有没有办法删除读取按钮并获取相对于按下行的 ID?

【问题讨论】:

    标签: c# sqlite xamarin


    【解决方案1】:

    使用 ListView "SelectionChanged" 事件:

    <ListView SelectionMode="Single"
        ItemsSource="{Binding PersonCollection}"
        SelectedItem="{Binding SelectedPerson}"
        SelectionChanged="ListView_SelectionChanged">
    

    在你的代码后面:

            private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (e.CurrentSelection.Count != 0)
                {
                    var bc = this.BindingContext as  YourViewModelName;
                    // now you have bc.SelectedPerson and you can access it's properties
                    // Display what ever you want 
                    collection.SelectedItem = null; // if you want to clear selection
                }
            }
    

    【讨论】:

      【解决方案2】:

      当用户点击一个项目时,会触发两个事件,“ItemTapped”和“ItemSelected”。它们之间的区别在于“ItemSelected”只会在选择改变时触发。如果你想点击物品显示物品信息,“ItemTapped”是更好的选择。

      XAML:

      <ListView ItemsSource="{Binding People}"
                SelectionMode="Single"
                ItemSelected="ListView_ItemSelected"
                ItemTapped="ListView_ItemTapped">
      

      xaml.cs:

      private async void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
      {
          await DisplayAlert("Tapped", "Person Name: " + (e.Item as Person).Name, "OK");
      }
      
      private async void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
      {
          await DisplayAlert("Selected", "Person Name: " + (e.SelectedItem as Person).Name, "OK");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-26
        • 2014-09-07
        • 2017-08-14
        • 1970-01-01
        • 1970-01-01
        • 2017-01-16
        • 1970-01-01
        • 2016-10-14
        相关资源
        最近更新 更多