【问题标题】:Xamarin.iOS custom renderer calling SetNativeControl has a very slow performanceXamarin.iOS 自定义渲染器调用 SetNativeControl 的性能很慢
【发布时间】:2020-12-16 14:50:32
【问题描述】:

我已经构建了一个自定义渲染器来获取 iOS 上的 TableView,它使用来自 iOS 的 InsetGrouped TableViewStyle。使用以下自定义控件和渲染器可以正常工作:

public class InsetGroupedTableView : TableView
{
    public Action ForceUpdateView;
}

[assembly: ExportRenderer(typeof(InsetGroupedTableView), typeof(InsetGroupedTableViewRenderer))]
namespace MyApp.iOS.Renderers
{
    public class InsetGroupedTableViewRenderer : TableViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
        {
            base.OnElementChanged(e);
            if (Control == null)
                return;
            var formsTableView = Element as InsetGroupedTableViewRenderer;
            var groupTable = new UITableView(Control.Frame, UITableViewStyle.InsetGrouped);
            groupTable.ContentInset = new UIEdgeInsets(-10, 0, 0, 0);
            groupTable.Layer.CornerRadius = 20;
            groupTable.Source = Control.Source;
            SetNativeControl(groupTable);
            formsTableView.ForceUpdateView += ForceUpdate;
        }

        private void ForceUpdate()
        {
            var groupTable = new UITableView(Control.Frame, UITableViewStyle.InsetGrouped);
            groupTable.ContentInset = new UIEdgeInsets(-10, 0, 0, 0);
            groupTable.Layer.CornerRadius = 20;
            groupTable.Layer.MasksToBounds = true;
            groupTable.Source = Control.Source;
            SetNativeControl(groupTable);
            Console.WriteLine("Finished");
        }
    }
}

但是,我的应用程序中有一些表格,我正在这些表格中即时生成TableSections。当我用新控件替换自定义渲染器中的表格时,显然 Xamarin.Forms 无法再更新我的表格了。但是,每当我更新表格时,我只是使用委托再次调用上面的相同代码。

这有什么问题吗?因为它的性能很糟糕。加载附加表大约需要 1.5 秒。我使用了一些秒表来查看上面的代码是否执行得很慢,但它确实很快,只是在调用上面的代码之后,显示新的 Section 需要很长时间。

有没有人知道是否有更好的方法来做到这一点?

【问题讨论】:

  • tablviewcell 是否有繁重的 UI 需要渲染?
  • @JackHua-MSFT 大约 5-10 个视图单元格,其中大部分是标签和选取器。如果我有没有渲染器的表格,生成的部分会立即出现。
  • 能否分享更多自定义渲染器中的代码?
  • @JackHua-MSFT 我编辑了我的问题并输入了我的渲染器和自定义控件的完整代码。在我的共享代码中,我只是调用ForceUpdateView()
  • 为什么需要在 ForceUpdateView 中再次调用 SetNativeControl。看起来它总是在设置 NativeControl。

标签: xamarin xamarin.forms xamarin.ios


【解决方案1】:

SetNativeControl 只能在自定义渲染器中调用一次。

再次调用这个方法完全没用,这也是它这么慢的原因。

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 2021-06-22
    • 1970-01-01
    • 2016-10-24
    • 2014-07-25
    • 1970-01-01
    • 2022-01-10
    • 2017-06-09
    • 1970-01-01
    相关资源
    最近更新 更多