【发布时间】:2018-05-15 18:51:09
【问题描述】:
我创建了以下 SQLite 表。
public class Settings
{
public string Name { get; set; }
[MaxLength(255)]
public string Value { get; set; }
}
SQLite 表被以下代码填充。
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", e.ToString(), "OK");
}
}
然后将其放入具有以下 XAML 的 ListView
<?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>
我的问题是,当有人在 ListView 中按下“生命的意义”项时,我如何才能显示值 42?
我为 e.ToString() 尝试了不同的东西,例如 e.Item 但在 System.EventArgs 中不存在。我认为我需要检查与 System.EventArgs 不同的东西。我认为 System.EventArgs 被称为命名空间,这是正确的吗?我怎么知道我可以选择哪些其他有效选项?
【问题讨论】:
标签: c# xaml xamarin xamarin.forms xamarin.ios