【问题标题】:Force pivot item to preload before it's shown强制透视项目在显示之前预加载
【发布时间】:2015-05-11 10:25:53
【问题描述】:

我有一个带有多个 PivotItem 的 Pivot,其中一个包含一个画布,该画布将其项目放置在动态位置(取决于数据)。我得到了数据,我可以在用户选择这个项目之前把这些项目放在他们的位置上(这不是第一个支点)。但是,只有当我选择 PivotItem 时,画布才会呈现自身,因此您可以看到它在显示之前闪烁。

有没有办法强制画布在显示之前进行渲染,这样在用户看到它时一切都准备好了?

我的代码如下所示:

在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);
    }
}

画布不是普通画布。我创建了一个新画布,它根据项目的相对位置显示项目(我得到的位置范围为 0-99)。

逻辑发生在 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# windows-runtime windows-phone-8.1 winrt-xaml


    【解决方案1】:

    您可以尝试几种技巧。例如:

    1. 在您的 ArrangeOverride 中,如果自上次执行后大小未更改(并且数据相同),您可以将逻辑短路
    2. 确保您正在收听 Pivot 上的事件,告诉您准备好进行演示 - 例如 PivotItemLoading
    3. 您可以让控件实际上不是Pivot 的一部分,而是位于父容器中(例如Grid)并使其Opacity 为零。然后在目标 PivotItem 出现时将其设置为 100。

    【讨论】:

    • 它没有 100% 工作,但我让它工作得更好。我把安排短路了,但是我用一个标志做的,并在安排完成后触发了一个事件。我在枢轴中捕捉到事件并隐藏我放置的进度条,然后显示画布。感谢您的帮助。
    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2016-01-06
    相关资源
    最近更新 更多