事件处理程序返回空 c#?

Event Handler returning null c#?

我正在 Xamarin.Android 中实现刷卡视图。我正在从 RatingCardAdapter 触发一个事件,以便在刷完所有卡片后重置刷卡视图。第一次,事件不为空,刷卡被重置,但在第二次尝试时,事件处理程序 returns 为空。结果,我无法设置 shouldResetSwipe 的值。我该如何解决这个问题?

适配器

public class RatingCardAdapter : BaseCardAdapter
{
private Context context; 
public event EventHandler OnLastCardSwiped;    
public RatingCardAdapter(Context context, SwipeCardsView SwipeView)
{
    this.context = context;
    this.SwipeView = SwipeView;
    SwipeView.SetCardsSlideListener(this);
}
public void OnCardVanish(int p0, SwipeCardsView.SlideType p1)
{
if (p0 == (Count - 1)) // p0 becomes 4 when last card is swiped
{
if (OnLastCardSwiped != null) //becomes null when rating adapter called 2nd time 
    OnLastCardSwiped(this, new OnLastCardSwipeArgs 
      { 
        shouldResetSwipe = true });
      } 
}
public class OnLastCardSwipeArgs : EventArgs
{
    public bool shouldResetSwipe { get; set; }
}

Activity

private SwipeCardsView swipeCardsView; 
RatingCardAdapter ratingCardAdapter;
protected override void OnCreate(Bundle savedInstanceState)    
{            
    base.OnCreate(savedInstanceState); 
    SetContentView(Resource.Layout.activity_rating_session);
    swipeCardsView = FindViewById<SwipeCardsView>             
                    (Resource.Id.swipeCardsRating);                                                
    swipeCardsView.RetainLastCard(false);                
    swipeCardsView.EnableSwipe(true);     
    setSwipeData();
 }    
void setSwipeData() {      
    ratingCardAdapter = new RatingCardAdapter(this, swipeCardsView); 
    swipeCardsView.SetAdapter(ratingCardAdapter); 
    ratingCardAdapter.OnLastCardSwiped += (sender, e) =>  
       {   
       if (e.shouldResetSwipe) 
        {
            Console.WriteLine("restart set " + e.shouldResetSwipe); 
            restartSwipeCard();     
        }}; 
 } 
 void restartSwipeCard() 
    {           
    Console.WriteLine("restartswipe"); 
    ratingCardAdapter = new RatingCardAdapter(this,swipeCardsView);  
    swipeCardsView.SetAdapter(ratingCardAdapter);        
    } 

由于您在 restartSwipeCard 方法中创建了 RatingCardAdapter 的新实例,因此您也需要订阅它的事件,因为它与 RatingCardAdapter 实例相关联。因此,将您的 lambda 方法移动到实例方法以防止代码重复并在 restartSwipeCard 方法中进行相同的订阅:

void restartSwipeCard() 
{           
    Console.WriteLine("restartswipe"); 
    ratingCardAdapter = new RatingCardAdapter(this,swipeCardsView);  
    swipeCardsView.SetAdapter(ratingCardAdapter);

    // need this subscription here too
    ratingCardAdapter.OnLastCardSwiped += (sender, e) =>  
    {   
        if (e.shouldResetSwipe) 
        {
            Console.WriteLine("restart set " + e.shouldResetSwipe); 
             restartSwipeCard();     
        }}; 
}

或者更好地将 setSwipeData 重命名为 initSwipeData 并像这样更新代码:

private SwipeCardsView swipeCardsView; 
RatingCardAdapter ratingCardAdapter;

protected override void OnCreate(Bundle savedInstanceState)    
{            
    base.OnCreate(savedInstanceState); 
    SetContentView(Resource.Layout.activity_rating_session);
    swipeCardsView = FindViewById<SwipeCardsView>             
                    (Resource.Id.swipeCardsRating);                                                
    swipeCardsView.RetainLastCard(false);                
    swipeCardsView.EnableSwipe(true);     
    initSwipeData();
 }    

private void initSwipeData() 
{      
    ratingCardAdapter = new RatingCardAdapter(this, swipeCardsView); 
    swipeCardsView.SetAdapter(ratingCardAdapter); 
    ratingCardAdapter.OnLastCardSwiped += (sender, e) =>  
       {   
       if (e.shouldResetSwipe) 
        {
            Console.WriteLine("restart set " + e.shouldResetSwipe); 
            Console.WriteLine("restartswipe"); 
            initSwipeData();    
        }}; 
 }