【问题标题】:Access Value From ObservableCollection from ListView从 ListView 访问 ObservableCollection 的值
【发布时间】: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


    【解决方案1】:

    尝试在“ItemSelected”事件中执行此操作

        private void MyItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var selected = e.SelectedItem as Settings;
            DisplayAlert("Alert", selected.Value, "OK");
        }
    

    【讨论】:

      【解决方案2】:

      Anastasia's answer 几乎是对的。我改用ItemTappedEvent 并进行了一些小调整以访问该对象:

      private void MyItemSelected(object sender, Xamarin.Forms.ItemTappedEventArgs e)
      {
          var settings = e.Item as Settings;
          DisplayAlert("Alert",  settings.Value, "OK");
      }
      

      【讨论】:

      • 我想要做的是,当您按下 ListView 中的某个项目时,它将显示一个 DisplayAlert,其中包含来自按下的 ListView 的关联值。可以在 ObservableCollection 中找到。
      • 我可以得到否决票的解释,以便我改进我的答案吗?代码应该可以工作。
      • 我没有对你投反对票,我什至没有足够的代表来投反对票。
      • 我不完全确定为什么,但您的解决方案在“var settings = textcell.BindingContext as Settings;”处抛出以下错误“System.NullReferenceException has been throwed”
      • 抱歉,出于某种原因,我假设您使用的是TapGestureRecognizer。请查看我编辑的答案,然后重试。
      猜你喜欢
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多