2017-08-07 7 views
0

私はXamarin.Androidでスワイプカードビューを実装しています。 RatingCardAdapterからすべてのカードをスワイプした後、スワイプカードビューをリセットするイベントを発生させています。初めてイベントがnullでなくスワイプカードがリセットされますが、2回目の試行ではイベントハンドラはnullを返します。その結果、私はの値を設定することはできませんshouldResetSwipe。どうすればこの問題を解決できますか?nullを返すイベントハンドラ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。

あるいはさらに良いinitSwipeDatasetSwipeDataの名前を変更し、このようなコードを更新:だからあなたのラムダメソッドは、コードの重複を防ぎ、restartSwipeCardメソッド内で同じサブスクリプションを行うための方法をintanceする移動

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

関連する問題