【发布时间】:2018-04-25 20:42:14
【问题描述】:
我有一个从 SQLite DB 填充的列表视图。对于那个 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,并将该对象发送到名为ViewSaved 的新视图。
不幸的是,我的变量 calculation 仍然为空,当我将空对象发送到我的新视图时出现异常。
我怀疑sender as MenuItem; 是问题所在。
【问题讨论】:
-
请显示单元格的相关代码/xaml 及其绑定
-
我已经更新了我的问题
-
当你调试这个时,
viewCellSelected会返回为空吗? -
ViewCellSelected返回为 null yes
标签: c# listview xamarin xamarin.forms