2017-10-17 29 views
0

J'ai écrit du code pour dessiner quelque chose dans une application iOS, si le serveur lui envoie de nouvelles coordonnées.La fonction de rappel se termine après la création de l'instance de la classe (C#)

J'ai une fonction de rappel qui dessine les coordonnées. Mais lorsque je crée une nouvelle instance d'une classe dans cette fonction, la fonction de rappel se ferme sans erreur ...

Quelqu'un d'autre a-t-il déjà eu ce problème?

Voici mon code si cela aide

CGPath pathtotal; 
    List<CGPath> path; 

    CGPoint initialPoint; 
    CGPoint latestPoint; 

    DrawDrawerDraw drawDrawerDraw; 

    public DrawGuessView(IntPtr handle) : base(handle) 
    { 
     BackgroundColor = UIColor.White; 
     pathtotal = new CGPath(); 

     SocketEventHandler.Add("draw:drawer:draw", onDrawDrawerDraw); 
    } 

    public void onDrawDrawerDraw(dynamic obj) 
    { 
     drawDrawerDraw = (DrawDrawerDraw)obj; 

     for (int i = 0; i <= drawDrawerDraw.coords.Count; i++) 
     { 
      if (initialPoint.X != (nfloat)drawDrawerDraw.coords[i].x0 && initialPoint.Y != (nfloat)drawDrawerDraw.coords[i].y0) 
      { 
       path[i] = new CGPath(); 
      } 

      initialPoint.X = (nfloat)drawDrawerDraw.coords[i].x0; 
      initialPoint.Y = (nfloat)drawDrawerDraw.coords[i].y0; 

      latestPoint.X = (nfloat)drawDrawerDraw.coords[i].x1; 
      latestPoint.Y = (nfloat)drawDrawerDraw.coords[i].y1; 

      //add lines to the touch points 
      if (path[i].IsEmpty) 
      { 
       path[i].AddLines(new CGPoint[] { initialPoint, latestPoint }); 
      } 
      else 
      { 
       path[i].AddLineToPoint(latestPoint); 
      } 
     } 

     SetNeedsDisplay(); 
    } 

    public override void Draw(CGRect rect) 
    { 
     base.Draw(rect); 

     try 
     { 
      foreach (var item in path) 
      { 
       if (!initialPoint.IsEmpty) 
       { 
        //get graphics context 
        using (CGContext g = UIGraphics.GetCurrentContext()) 
        { 
         //set up drawing attributes 
         g.SetLineWidth(2); 
         UIColor.Black.SetStroke(); 

         //add geometry to graphics context and draw it 
         pathtotal.AddPath(item); 

         g.AddPath(pathtotal); 
         g.DrawPath(CGPathDrawingMode.Stroke); 
        } 
       } 
      } 
     } 
     catch (Exception e) { } 
    } 
} 
+1

Pouvez-vous localiser la ligne qui est à l'origine l'exception? Déboguer ligne par ligne si nécessaire. –

+0

Le problème est ici: path [i] = new CGPath(); – Blanko

+0

Toujours quand je crée une nouvelle instance de quelque chose ... – Blanko

Répondre

1

Il y a deux points que vous devez modifier.

  1. Initialiser path dans DrawGuessView méthode

    public DrawGuessView(IntPtr handle) : base(handle) 
    { 
        BackgroundColor = UIColor.White; 
        pathtotal = new CGPath(); 
        List<CGPath> path = new List<CGPath>(); 
        SocketEventHandler.Add("draw:drawer:draw", onDrawDrawerDraw); 
    } 
    
  2. path[i] = new CGPath() provoquera la valeur définie ArgumentOutOfRangeException, nous ne pouvons pas l'élément dans la liste de cette façon.

    Modifier la boucle

    CGPath pathItem = null; 
    for (int i = 0; i <= drawDrawerDraw.coords.Count; i++) 
    { 
        if (initialPoint.X != (nfloat)drawDrawerDraw.coords[i].x0 && initialPoint.Y != (nfloat)drawDrawerDraw.coords[i].y0) 
        { 
         pathItem = new CGPath(); 
        } 
    
        initialPoint.X = (nfloat)drawDrawerDraw.coords[i].x0; 
        initialPoint.Y = (nfloat)drawDrawerDraw.coords[i].y0; 
    
        latestPoint.X = (nfloat)drawDrawerDraw.coords[i].x1; 
        latestPoint.Y = (nfloat)drawDrawerDraw.coords[i].y1; 
    
        //add lines to the touch points 
        if (pathItem.IsEmpty) 
        { 
         pathItem.AddLines(new CGPoint[] { initialPoint, latestPoint }); 
        } 
        else 
        { 
         pathItem.AddLineToPoint(latestPoint); 
        } 
        path.Add(pathItem); 
    } 
    
+0

Maintenant, j'ai le même problème sur cette ligne: 'path.Add (pathItem);' – Blanko

+0

Je pense que le code n'a pas entrer dans la condition 'initialPoint.X ! = (nfloat) drawDrawerDraw.coords [i] .x0 && initialPoint.Y! = (nfloat) drawDrawerDraw.coords [i] .y0', donc ajoutez l'objet 'null ', vous devez changer la condition. par exemple. voir ma mise à jour –

+0

Non, le code entre dans la condition et continue jusqu'à ce que je souhaite l'ajouter au chemin. – Blanko