3

我实现了一个 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
      { }
   }
}
4

1 回答 1

0

看起来唯一的解决方案是将耗时的绘图和实际控件上的绘图分开。

解决方案是

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

至少它对我有用。

于 2016-04-21T13:05:32.363 回答