亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在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 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號