2017-08-07 172 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