如何在滑动视图中获取当前项目

时间:2021-07-23 13:44:48

标签: c# xaml xamarin.forms

我想在滑动视图中获取当前项目,但它不起作用。

当点击 OnSwipeDelete 时,代码停止,我有错误提示:System.NullReferenceException: 'Object reference not set to an instance of an object.'

好像没get到Mywords的值

这是我的 Xaml 代码(我的 Xaml 代码在注释中,以便能够在 stackoverflow 上发布):

     <!--   
        <ListView x:Name="WordSList" >
        <ListView.ItemTemplate>
        <DataTemplate>
            <custom:CustomViewCell SelectedItemBackgroundColor="White">
                <ViewCell.View >
                    <SwipeView>
                        <SwipeView.RightItems>  
                            <SwipeItems>
                         
                                <SwipeItem Text="Edit" BackgroundColor="#7D7D7D" ></SwipeItem>
                                <SwipeItem Text="Delete"   Invoked="OnSwipeDelete">BackgroundColor="Red"      ></SwipeItem>
        
                            </SwipeItems> 
        
                        </SwipeView.RightItems> 
        
                        <SwipeView.Content >  
                            <StackLayout >
        
                               
                                <Label TextColor="#7D7D7D" Text="{Binding Word1}"  FontSize="15" />
                            </StackLayout>  
                        </SwipeView.Content>  
        
                    </SwipeView>  
                </ViewCell.View>  
        
            </custom:CustomViewCell>
        </DataTemplate>
        
        </ListView.ItemTemplate>
        </ListView>  
 -->
 

这是我的 C# 代码:

  private async void OnSwipeDelete(object sender, EventArgs e)
        {

            // Get Item
            SwipeItemView item = sender as SwipeItemView;
            // Cas to MyWords
            MyWords mywordinfos = item.BindingContext as MyWords;


             // display the select word
            Console.WriteLine("See word " + mywordinfos.Word1);
        }

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

这是答案。谢谢杰森;)

private async void OnSwipeDelete(object sender, EventArgs e)
{
    SwipeItem item = sender as SwipeItem;
    var mywordinfos = item.BindingContext as MyWords;

}
相关问题