我有一个继承自DialogFragment
的{{1}}类,它显示了可用(未定位)玩家的列表。
当另一个名为UnseatedPlayers
的班级在ActiveTable
对话框片段上单击空座位时出现,但当我按回时,对话框片段消失(按预期)并点击任意一个免费座位和2个对话框片段相互堆叠,如果我重复过程4对话框片段,然后是8等等。
我认为这与我每次点击座位时创建一个新的对话框片段有关:
UnseatedPlayers
但我不确定,当我按下它时,它应该以正确的方式解除对话框片段,并根据this帖子:
关闭片段及其对话框。如果片段被添加到 返回堆栈,所有后向堆栈状态直到并包括此条目 被弹出。否则,将提交一个新事务以删除 片段。
为什么对话框片段彼此出现?
ActiveTable.cs(这是实例化UnseatedPlayers的地方):
private void FreeSeatPlayer_Click(object sender, EventArgs e)
{
var button = sender as Button;
int seatId = (int)button.Tag;
var trans = this.FragmentManager.BeginTransaction();
var unseatedPlayers = new UnseatedPlayers(seatId);
unseatedPlayers.OnDimissEvent += UnseatedPlayers_OnDimissEvent;
unseatedPlayers.Show(trans, "unseated_players");
}
UnseatedPlayers.cs
namespace Droid.Pages.Session
{
public class ActiveTable : Page
{
public TableListItemViewModel Model { get; set; }
public List<Button> Seats { get; set; }
public ActiveTable(FragmentManager fragmentManager, int resourceId, TableListItemViewModel model)
{
FragmentManager = fragmentManager;
base.ResourceId = resourceId;
Model = model;
base.Name = model.Name;
}
public override View Draw(ViewGroup container)
{
View view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.session, container, false);
container.AddView(view);
var root = view.FindViewById<LinearLayout>(Resource.Id.sessionBody);
Context context = view.Context;
Seats = new List<Button>();
foreach (var seat in Model.Seats)
{
Button btn = new Button(context);
btn.Tag = seat.Id;
DrawSeat(btn, seat);
root.AddView(btn);
Seats.Add(btn);
}
return view;
}
private Button DrawSeat(Button button, SeatListItemViewModel seat)
{
if (seat.Occupied)
return DrawOccupiedSeat(button, seat);
else
return DrawFreeSeat(button, seat);
}
private Button DrawOccupiedSeat(Button button, SeatListItemViewModel seat)
{
button.SetBackgroundResource(Resource.Drawable.button_default);
if (seat.SeatType == "Player")
{
button.Text = seat.User.Name;
button.Click += OccupiedSeatPlayer_Click;
return button;
}
else if(seat.SeatType == "Dealer")
{
button.Text = "DEALER: " + seat.User.Id.ToString();
button.Click += OccupiedSeatDealer_Click;
return button;
}
throw new NotImplementedException();
}
private Button DrawFreeSeat(Button button, SeatListItemViewModel seat)
{
button.Text = seat.SeatNumber.ToString() + ": Sentar";
button.SetBackgroundResource(Resource.Drawable.button_green);
if(seat.SeatType == "Player")
button.Click += FreeSeatPlayer_Click;
else if(seat.SeatType == "Dealer")
button.Click += FreeSeatDealer_Click;
return button;
}
private void RedrawSeats()
{
Model = A.GetCustomer().Tables.Where(c => c.Id == Model.Id).FirstOrDefault();
foreach (var btn in Seats)
{
var id = (int)btn.Tag;
var seat = Model.Seats.Where(c => c.Id == id).FirstOrDefault();
DrawSeat(btn, seat);
}
}
/* Events */
private void OccupiedSeatPlayer_Click(object sender, EventArgs e)
{
var button = sender as Button;
int seatId = (int)button.Tag;
var seat = Model.Seats.Where(c => c.Id == seatId).FirstOrDefault();
var player = A.GetPlayer(seat.User.Id);
var playerSession = player.GetActivePlayerSession();
var trans = FragmentManager.BeginTransaction();
var frag = new Fragments.PlayerSessions.PlayerSession(playerSession);
trans.Replace(base.ResourceId, frag, "player_session");
trans.AddToBackStack("player_session");
trans.Commit();
}
private void FreeSeatPlayer_Click(object sender, EventArgs e)
{
var button = sender as Button;
int seatId = (int)button.Tag;
var trans = this.FragmentManager.BeginTransaction();
var unseatedPlayers = new UnseatedPlayers(seatId);
unseatedPlayers.OnDimissEvent += UnseatedPlayers_OnDimissEvent;
unseatedPlayers.Show(trans, "unseated_players");
}
private void FreeSeatDealer_Click(object sender, EventArgs e)
{
var button = sender as Button;
int seatId = (int)button.Tag;
var trans = this.FragmentManager.BeginTransaction();
var unseatedDealers = new UnseatedDealers(seatId);
unseatedDealers.OnDimissEvent += UnseatedDealers_OnDimissEvent;
unseatedDealers.Show(trans, "unseated_dealers");
}
private void OccupiedSeatDealer_Click(object sender, EventArgs e)
{
var button = sender as Button;
int seatId = (int)button.Tag;
var seat = A.GetSeat(seatId);
var dealer = A.GetDealer(seat.User.Id);
var dealerSession = dealer.DealerSessions.Where(c => String.IsNullOrEmpty(c.EndDate)).FirstOrDefault();
var trans = this.FragmentManager.BeginTransaction();
var closeDealerSession = new Fragments.Sessions.CloseDealerSession(dealerSession, seatId);
//unseatedPlayers.OnDimissEvent += UnseatedPlayers_OnDimissEvent;
closeDealerSession.Show(trans, "close_dealer_session");
}
private void UnseatedDealers_OnDimissEvent(object sender, bool e)
{
RedrawSeats();
}
private void UnseatedPlayers_OnDimissEvent(object sender, bool e)
{
RedrawSeats();
}
}
}