Core Graphics calls within a Xamarin Forms iOS custom renderer

lzcdlzcd USMember

Hi,

I'm working on a project that is utilising Xamarin Forms manly for future maintainability by a given team (rather than cross platform concerns).

Within this project, I'm needing to render a reasonably complex set of graphics for a control.
It's not a chart but a chart is an equally valid example of such a need.

I have working Core Graphics code from an older Xamarin "Classic" app and a working custom renderer in a new Xamarin Forms app.
Now the challenge is to get the two pieces of code to play nicely together.

If I pick BoxView and BoxRenderer as my base classes for my custom control and renderer respectively then my override of the Draw method certainly gets called but I haven't had any luck getting it display anything onscreen beyond a black (via a ClearRect call with a different Fill colour).

Is it possible to utilise Core Graphics within a custom renderer? .. and if so, would somebody be able to point me towards an example?

Regards,
Luke

Best Answer

Answers

  • adamkempadamkemp USInsider, Developer Group Leader mod

    It's certainly possible, but it would probably be more productive for you to just show us what you're trying. There's probably something you're doing wrong or missing, and if you show us your code we can probably spot the issue. Attach a .zip of a sample project that shows what you're trying.

  • lzcdlzcd USMember
    edited June 2015

    I'm at the mercy of some extremely draconian NDAs and the like so the best I can offer is some demo code that illustrates the sort of code I would like to have work.

    Here's an example of a custom control on the Froms' side:

    public class CustomControlExample : BoxView
        {
            public CustomControlExample () 
            {
            }
        }
    

    (I picked BoxView just because it seemed the simplest control but I'm certainly not tied to it)

    And here's my iOS custom renderer:

    [assembly: ExportRenderer (typeof (CustomControlExample), typeof (IosCustomControlRendererExample))]
    ...
    
    public class IosCustomControlRendererExample : BoxRenderer
    {
        public override void Draw (CoreGraphics.CGRect rect)
        {
            using (var g = UIGraphics.GetCurrentContext ())
            {
                g.SaveState ();
    
                UIColor.Brown.SetFill ();
    
                g.ClearRect (this.Bounds);
    
                var path = new CGPath ();
                path.AddLines(new CGPoint[] {
                    new CGPoint(1,1),
                    new CGPoint(200,1),
                    new CGPoint(1,200)
                });
    
                path.CloseSubpath ();
                g.SetLineWidth (4);
                UIColor.Purple.SetStroke ();
                UIColor.Red.SetFill ();
                g.DrawPath (CGPathDrawingMode.FillStroke);
    
    
                g.RestoreState ();
            }
            //base.Draw (rect);
    
        }
    }
    

    (I've tried various alternatives of when, where and if I can the base.Draw method)

  • adamkempadamkemp USInsider, Developer Group Leader mod

    I'm not asking for your real code. Just create a new solution and put this code in. Any lawyer who thinks what you just shared is ok but what I'm asking for isn't should be fired. Don't make me do the work for you.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    This is important because there's a decent chance the bug is not in the code you quoted. It doesn't help seeing a small subset of code out of context.

  • lzcdlzcd USMember

    Example solution attached.
    (Nuget packages not included so they'll need to be re-downloaded upon extraction)

Sign In or Register to comment.