为了账号安全,请及时绑定邮箱和手机立即绑定

在ListView Tappedevent上获取对象

在ListView Tappedevent上获取对象

C#
翻阅古今 2021-05-06 18:16:56
我有一个从SQLite数据库填充的列表视图。对于该listview,构造了一种方法,用于在敲击listview元素时进行处理:xaml单元格:<ListView x:Name="CalculationListview" ItemsSource="{Binding Calculation}" HasUnevenRows="true" IsPullToRefreshEnabled="true" Refreshing="Handle_Refreshing">    <ListView.ItemTemplate>        <DataTemplate>            <ViewCell Tapped="Handle_Tapped">                <ViewCell.ContextActions>                    <MenuItem Text="Delete" IsDestructive="True" Clicked="Handle_Clicked" />                </ViewCell.ContextActions>                <StackLayout>                    <Label Text="{Binding Qty}">                    </Label>                    <Label Text="{Binding Note}">                    </Label>                    <Label Text="{Binding Id}">                    </Label>                </StackLayout>            </ViewCell>        </DataTemplate>    </ListView.ItemTemplate></ListView>C#async void Handle_Tapped(object sender, System.EventArgs e){    var viewCellSelected = sender as MenuItem;    var calculation = viewCellSelected?.BindingContext as Calculation;    var page = new ViewSaved(calculation);    await Navigation.PushModalAsync(page);}如您所见,我想Calculation从给定的listview元素中检索对象并将该对象发送到名为的新视图ViewSaved。不幸的是,我的变量calculation仍然为null,并且在将空对象发送到新视图时收到异常。我怀疑这sender as MenuItem;是问题所在。
查看完整描述

1 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

而不是将Tapped处理程序放在上ViewCell,请使用ListView's ItemTapped或ItemSelected方法


void Handle_ItemTapped(object sender, ItemTappedEventArgs e)

    {

      var item = (Calculation)i.Item;

    }


查看完整回答
反对 回复 2021-05-15
  • 1 回答
  • 0 关注
  • 149 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信