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);
}
謝謝由良,你是一個救世主。 –
很高興幫助你!不要忘記標記答案是正確的) – Yura