Tôi có Pivot với một vài PivotItems, một trong số đó chứa canvas đặt các mục của nó ở vị trí động (tùy thuộc vào dữ liệu). Tôi nhận dữ liệu và tôi có thể đặt các mục vào vị trí của mình trước khi người dùng có thể chọn mục này (đây không phải là trục đầu tiên). Tuy nhiên, chỉ khi tôi chọn PivotItem, canvas sẽ hiển thị chính nó, vì vậy bạn có thể thấy nó nhấp nháy trước khi nó được hiển thị như nó cần.Buộc mục trục để tải trước trước khi hiển thị
Có cách nào để buộc canvas hiển thị trước khi hiển thị, vì vậy mọi thứ được chuẩn bị trước khi người dùng nhìn thấy nó?
Mã của tôi trông giống như sau:
Trong page.xaml.cs:
private async void GameCenterView_OnDataContextChanged(object sender, EventArgs e)
{
// Load data...
// Handle other pivots
// This is the problem pivot
if (ViewModel.CurrentGame.SportTypeId == 1)
{
_hasLineups = ViewModel.CurrentGame.HasLineups.GetValueOrDefault();
HasFieldPositions = ViewModel.CurrentGame.HasFieldPositions.GetValueOrDefault();
// I only add the pivot when I need it, otherwise, it won't be shown
if (_hasLineups)
{
if (MainPivot.Items != null) MainPivot.Items.Add(LineupPivotItem);
}
if (HasFieldPositions)
{
// Here I place all the items in their proper place on the canvas
ArrangeLineup(ViewModel.TeamOneLineup, TeamOneCanvas);
ArrangeLineup(ViewModel.TeamTwoLineup, TeamTwoCanvas);
}
}
// Handle other pivots
}
private void ArrangeLineup(ObservableCollection<PlayerInLineupViewModel> teamLineup, RationalCanvas canvas)
{
if (teamLineup == null)
return;
foreach (var player in teamLineup)
{
var control = new ContentControl
{
Content = player,
ContentTemplate = LinupPlayerInFieldDataTemplate
};
control.SetValue(RationalCanvas.RationalTopProperty, player.Player.FieldPositionLine);
control.SetValue(RationalCanvas.RationalLeftProperty, player.Player.FieldPositionSide);
canvas.Children.Add(control);
}
}
Các vải không phải là vải cổ phiếu. Tôi đã tạo một canvas mới hiển thị các mục theo vị trí tương đối của chúng (tôi nhận được các vị trí theo thang tỷ lệ 0-99).
Logic xảy ra trong phương pháp OverrideArrange:
protected override Size ArrangeOverride(Size finalSize)
{
if (finalSize.Height == 0 || finalSize.Width == 0)
{
return base.ArrangeOverride(finalSize);
}
var yRatio = finalSize.Height/100.0;
var xRatio = finalSize.Width/100.0;
foreach (var child in Children)
{
var top = (double) child.GetValue(TopProperty);
var left = (double) child.GetValue(LeftProperty);
if (top > 0 || left > 0)
continue;
var rationalTop = (int) child.GetValue(RationalTopProperty);
var rationalLeft = (int) child.GetValue(RationalLeftProperty);
if (InvertY)
rationalTop = 100 - rationalTop;
if (InvertX)
rationalLeft = 100 - rationalLeft;
child.SetValue(TopProperty, rationalTop*yRatio);
child.SetValue(LeftProperty, rationalLeft*xRatio);
}
return base.ArrangeOverride(finalSize);
}
Cảm ơn.
Nó không hoạt động 100%, nhưng tôi đã làm việc tốt hơn. Tôi đã rút ngắn sự sắp xếp, nhưng tôi đã làm nó với một lá cờ, và sa thải một sự kiện khi sắp xếp được thực hiện. Tôi nắm bắt sự kiện trong trục và ẩn thanh tiến trình tôi đã đặt, sau đó hiển thị canvas. Cảm ơn đã giúp đỡ. – CKII