C# 如何使用а;使用xamarin从SQLite自动分配ID
我有一个输入ID的字段和一个行读取按钮。 当您写入ID并单击按钮时,会出现一条消息,其中相应的行上有信息。 我想删除按钮“Read”和输入字段,这样当用户单击相应的行时,将显示一条包含该行信息的消息 我的xaml.cs代码是:C# 如何使用а;使用xamarin从SQLite自动分配ID,c#,sqlite,xamarin,C#,Sqlite,Xamarin,我有一个输入ID的字段和一个行读取按钮。 当您写入ID并单击按钮时,会出现一条消息,其中相应的行上有信息。 我想删除按钮“Read”和输入字段,这样当用户单击相应的行时,将显示一条包含该行信息的消息 我的xaml.cs代码是: async void ReadCity() { if (!string.IsNullOrEmpty(txtPersonId.Text)) { //Get Person var pers
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?使用ListView“SelectionChanged”事件:
使用ListView“SelectionChanged”事件:
当用户点击一个项目时,将触发两个事件,“ItemTapped”和“ItemSelected”。它们之间的区别在于,“ItemSelected”仅在选择更改时才会触发。如果您想单击项目以显示项目信息,“ItemTapped”是一个更好的选择 XAML:
当用户点击一个项目时,将触发两个事件,“ItemTapped”和“ItemSelected”。它们之间的区别在于,“ItemSelected”仅在选择更改时才会触发。如果您想单击项目以显示项目信息,“ItemTapped”是一个更好的选择 XAML:
<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
}
}
<ListView ItemsSource="{Binding People}"
SelectionMode="Single"
ItemSelected="ListView_ItemSelected"
ItemTapped="ListView_ItemTapped">
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");
}