重写 ViewRenderer 块的绘制方法 UI

Overridden Draw method of ViewRenderer blocks UI

我实施了一个 Xamarin.Forms 控制。我目前遇到的问题是自定义渲染器的重写 Draw() 方法块 UI(至少对于 iOS 平台)。我用谷歌搜索但没有成功。是否可以在不阻塞 UI?

的情况下在后台执行绘图

这里是 iOS 平台的简单渲染器代码,它演示了这个问题。

public class MyCustomRenderer : ViewRenderer
{
   protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
   {
      base.OnElementPropertyChanged(sender, e);
      SetNeedsDisplay();
   }

   public override void Draw(CoreGraphics.CGRect rect)
   {
      var myControl = (MyControl)this.Element;

      if (!myControl.IsRendered)
      {
         using (var context = UIGraphics.GetCurrentContext())
         {
            var token = CancellationToken.None;
            var task = Task.Factory.StartNew(() => TimeConsumingRendering(context, token), token);

            // task.Wait() blocks the UI but draws the desired graphics.
            // When task.Wait() is commented out = the desired graphics doesn't get drawn and it doesn't block the UI
            task.Wait();
         }
      }
   }

   private void TimeConsumingRendering(CGContext context, CancellationToken token)
   {
      try
      {
         for (int i = 0; i <= 100; i++)
         {
            token.ThrowIfCancellationRequested();
            var delay = Task.Delay(50);
            delay.Wait();
         }

         context.ScaleCTM(1f, -1f);
         context.TranslateCTM(0, -Bounds.Height);
         context.SetTextDrawingMode(CGTextDrawingMode.FillStroke);
         context.SelectFont("Helvetica-Bold", 16f, CGTextEncoding.MacRoman);
         context.SetFillColor(new CoreGraphics.CGColor(1f, 0f, 0f));
         context.ShowTextAtPoint(0, 0, "Finished");
      }
      catch
      { }
   }
}

看来唯一的解决办法就是将耗时的绘图和在实际控件上绘图分开。

解决方案是

  1. 在背景中生成图像(由事件触发)。并且只能在 Draw 方法中使用它。
  2. 在覆盖的 Draw 方法中使用生成的图像。

至少对我有用。