我正在Xamarin.Android中实现刷卡视图。我将从 RatingCardAdapter 中触发一个事件,以便在刷完所有卡后重置刷卡视图。第一次,事件不为null并且刷卡被重置,但在第二次尝试时,事件处理程序返回null。因此,我无法设置 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; }
}
活动
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 :(得分:0)
由于您在restartSwipeCard
方法中创建了一个新的RatingCardAdapter实例,因此您还需要订阅它的事件,因为它与RatingCardAdapter intance相关联。因此,将lambda方法移动到intance方法以防止代码重复并在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();
}};
}