2016-04-08 6 views
3

Xamarin.Formsコントロールを実装しています。私が現在経験している問題は、カスタムレンダラのオーバーライドされたDraw()メソッドが(少なくともiOSプラットフォームでは)UIをブロックするということです。私はグーグルだが、成功していない。 UIをブロックすることなく、バックグラウンドで図面を実行することは可能ですか?ViewRendererブロックの描画メソッドをオーバーライドしました。

ここに、問題を示す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 
     { } 
    } 
} 

答えて

0

実際の制御では時間がかかる描画と描画を分離することが唯一の解決策であるように見えます。

溶液(イベントによってトリガ)背景画像を生成する

  1. あります。 Drawメソッド内でのみ使用してください。
  2. オーバーライドされたDrawメソッド内で生成されたイメージを使用する。

少なくともそれは私のために働く。

関連する問題