【问题标题】:Improve performance during add controls on Windows Phone 8.1在 Windows Phone 8.1 上添加控件时提高性能
【发布时间】:2016-01-13 19:44:12
【问题描述】:

在向网格添加控件时,我遇到了性能问题。当我设置添加 100 个控件时,它在 Windows Phone 中需要将近 2 秒,它会冻结 UI。有没有其他方法可以将自定义控件添加到网格?这是我的代码:

private void generateGrid(int size)
{
    int buttonsInColumn = 5; 
    SmallButtonControl smallButton;
    buttonEdge = screenWidth / buttonsInColumn;

    for (int i = 0; i < buttonsInColumn; i++)
        grChoolseBoard.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
    for (int j = 0; j < size / buttonsInColumn; j++)
        grChoolseBoard.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });

    for (int j = 0; j < size / buttonsInColumn; j++)
    {
        for (int i = 0; i < buttonsInColumn; i++)
        {
            smallButton = new SmallButtonControl() { Width = buttonEdge, Height = buttonEdge };
            smallButton.Text = (j * buttonsInColumn + i + 1).ToString();
            smallButton.ButtonBackground = getColorAndEnableState(j * buttonsInColumn + i + 1, _level+1,ref smallButton); 
            smallButton.SetValue(Grid.ColumnProperty, i);
            smallButton.SetValue(Grid.RowProperty, j);
            grChoolseBoard.Children.Add(smallButton as SmallButtonControl);
            smallButton.Tapped += new TappedEventHandler(GoToBoard_Tapped);
        }
    }
}

【问题讨论】:

    标签: c# xaml windows-phone-8.1 code-behind


    【解决方案1】:

    在 C# 窗体中,为避免在添加许多控件时出现延迟,SuspendLayout(); 在添加控件时忽略更新 UI,然后ResumeLayout(); 向 UI 发出信号以再次开始显示更改。我不确定 Windows Phone 开发是否具有相同的功能。

    【讨论】:

    • 感谢您的回答。我尝试使用await Dispatcher.RunAsync(CoreDispatcherPriority.High,但这还不够,因为将控件添加到布局的时间是相同的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    相关资源
    最近更新 更多