将子项添加到网格时锁定网格上的视觉更新



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

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

谢谢你的建议。

我认为布局计算在这里不是问题——这是第一次渲染,涉及太多步骤,如指定渲染器,所有这些都可能导致延迟。Visual树越大,情况就越糟。

您可以尝试将Grid与其Parent分离,进行更新,然后将其添加回预期的父对象(这应确保渲染更新处于挂起状态(。

//first detach from visual tree
var parent = currentGrid.Parent as ContentView;
parent.Conent = null;
//do your updates to grid control here
//now attach it back to visual tree
parent.Content = currentGrid;

另一个选项是(如@orhtej2所述(扩展Grid控件以挂起布局计算。但不确定它是否有助于渲染性能。

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;

最新更新