2017-08-07 127 views
0

我在Xamarin.Android中实现了刷卡视图。我正在从评级卡适配器发起一个事件,以在所有卡被刷卡后重置刷卡视图。这是第一次,事件不为空,刷卡被重置,但在第二次尝试时,事件处理程序返回null。因此,我无法设置值shouldResetSwipe。我怎么解决这个问题?事件处理程序返回空c#?

适配器

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; } 
} 

活动

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);   
    } 

回答

0

既然你是在restartSwipeCard方法创建RatingCardAdapter的新实例,您需要订阅过它的活动还,因为它绑RatingCardAdapter intance。因此,移动你的拉姆达方法intance方法,以防止重复代码,做内部restartSwipeCard方法相同的订阅:

甚至更​​好重命名setSwipeDatainitSwipeData和这样的更新代码:

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();  
     }}; 
} 
+0

谢谢由良,你是一个救世主。 –

+0

很高兴帮助你!不要忘记标记答案是正确的) – Yura