我有一個(gè)從SQLite數(shù)據(jù)庫(kù)填充的列表視圖。對(duì)于該listview,構(gòu)造了一種方法,用于在敲擊listview元素時(shí)進(jìn)行處理: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);}如您所見(jiàn),我想Calculation從給定的listview元素中檢索對(duì)象并將該對(duì)象發(fā)送到名為的新視圖ViewSaved。不幸的是,我的變量calculation仍然為null,并且在將空對(duì)象發(fā)送到新視圖時(shí)收到異常。我懷疑這sender as MenuItem;是問(wèn)題所在。
1 回答

慕哥6287543
TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊
而不是將Tapped處理程序放在上ViewCell,請(qǐng)使用ListView's ItemTapped或ItemSelected方法
void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
{
var item = (Calculation)i.Item;
}
- 1 回答
- 0 關(guān)注
- 157 瀏覽
添加回答
舉報(bào)
0/150
提交
取消