Xamarin.forms 在网格上锁定可视更新,同时向其中添加子项

Xamarin.forms 在网格上锁定可视更新,同时向其中添加子项,xamarin.forms,Xamarin.forms,将子项(单元格)添加到网格时,可以看到网格添加了行和列。 虽然一开始这看起来很酷,但它让应用程序看起来动作缓慢 因此,我想知道是否有办法锁定视觉更新,希望这也能加快网格填充速度 谢谢您的建议。我认为布局计算在这里不是问题-这是第一次渲染,涉及太多步骤,例如指定渲染器以及所有可能导致延迟的因素。视觉树越大,效果越差 您可以尝试将网格与其父网格分离,进行更新,然后将其添加回预期父网格(这将确保渲染更新被挂起) 另一个选项是(如@orhtej2所述)扩展网格控件以暂停布局计算。但不确定它是否有助于提高

将子项(单元格)添加到网格时,可以看到网格添加了行和列。 虽然一开始这看起来很酷,但它让应用程序看起来动作缓慢

因此,我想知道是否有办法锁定视觉更新,希望这也能加快网格填充速度


谢谢您的建议。

我认为布局计算在这里不是问题-这是第一次渲染,涉及太多步骤,例如指定渲染器以及所有可能导致延迟的因素。视觉树越大,效果越差

您可以尝试将
网格
与其
父网格
分离,进行更新,然后将其添加回预期父网格(这将确保渲染更新被挂起)

另一个选项是(如@orhtej2所述)扩展
网格
控件以暂停布局计算。但不确定它是否有助于提高渲染性能

public class SmartGrid : Grid
{
    public static readonly BindableProperty SuspendLayoutProperty =
        BindableProperty.Create(
        "SuspendLayout", typeof(bool), typeof(SmartGrid),
        defaultValue: default(bool), propertyChanged: OnSuspendLayoutChanged);

    public bool SuspendLayout
    {
        get { return (bool)GetValue(SuspendLayoutProperty); }
        set { SetValue(SuspendLayoutProperty, value); }
    }

    static void OnSuspendLayoutChanged(BindableObject bindable, object oldValue, object newValue)
    {
        ((SmartGrid)bindable).OnSuspendLayoutChangedImpl((bool)oldValue, (bool)newValue);
    }

    protected virtual void OnSuspendLayoutChangedImpl(bool oldValue, bool newValue)
    {
        InvalidateLayout();
    }

    protected override void InvalidateLayout()
    {
        if(!SuspendLayout)
            base.InvalidateLayout();
    }

    protected override void InvalidateMeasure()
    {
        if (!SuspendLayout)
            base.InvalidateMeasure();
    }

    protected override void LayoutChildren(double x, double y, double width, double height)
    {
        if (!SuspendLayout)
            base.LayoutChildren(x, y, width, height);
    }
}
示例用法如下所示:

//first detach from visual tree
var parent = currentGrid.Parent as ContentView;
parent.Conent = null;

currentGrid.SuspendLayout = true;
//do your updates to grid control here
currentGrid.SuspendLayout = false;

//now attach it back to visual tree
parent.Content = currentGrid;

我唯一的建议是创建新的类继承
网格
,覆盖并在完成后强制布局更新。@orhtej2谢谢。你能把你的评论作为答案吗?也许还可以添加一些关于如何准确地做到这一点的更多信息(只有在您愿意的情况下,但这将是非常好的!)?
//first detach from visual tree
var parent = currentGrid.Parent as ContentView;
parent.Conent = null;

currentGrid.SuspendLayout = true;
//do your updates to grid control here
currentGrid.SuspendLayout = false;

//now attach it back to visual tree
parent.Content = currentGrid;