我有一個從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;
}
- 1 回答
- 0 關注
- 160 瀏覽
添加回答
舉報
0/150
提交
取消