1

I want to port this to C#.

I don't know how I should convert this:

@property (nonatomic, strong, readonly) CAGradientLayer *layer;

The layer is the default layer for the UIView. Xamarin already has a Layer property. How do I overwrite this? Do I need to overwrite it?

I also tried it with

public CAGradientLayer layer { [Export ("Layer")] get; [Export ("Layer:")] set; }

but the app crashes (System.Reflection.TargetInvocationException - NullReferenceException when dequeuing the cell) if I want to set the colors of the layer. Furthermore it should be readonly.

Then I saw in the documentation how it could be converted:

public class BlueView : UIView
{
    [Export ("layerClass")]
    public static Class GetLayerClass ()
    {
        return new Class (typeof (BlueLayer));
    }

    public override void Draw (RectangleF rect)
    {
        // Do nothing, the Layer will do all the drawing
    }
}

public class BlueLayer : CALayer
{
    public override void DrawInContext (CGContext ctx)
    {
        ctx.SetFillColor (0, 0, 1, 1);
        ctx.FillRect (Bounds);
    }
}

This doesn't help me because I need something like SetFillColors so that I can use CGColor[] array. But there is no such function.

How do I create my UIView with my custom CAGradientLayer in C# with Monotouch?

Community
  • 1
  • 1
testing
  • 19,681
  • 50
  • 236
  • 417
  • Please explain in more detail, why doesn't this help? Which SetFillColors method do you want to use, but can't? – Dimitris Tavlikos Nov 10 '14 at 11:20
  • There is only `SetFillColor` without the *s*. There are different `SetFillColor` methods available. One takes `float[] components`. Another one `CGColor`. There is no method which takes `CGColor[]`. – testing Nov 10 '14 at 11:26
  • I can't find a SetFillColor*s* method anywhere. Apparently, there is no such method available. However, since you are talking about a CAGradientLayer, override your class from that, instead of CALayer. The CAGradientLayer has a Colors property which is a CGColor[]. If I'm guessing correctly what you want to do... – Dimitris Tavlikos Nov 10 '14 at 11:36
  • Thanks for your response. A normal `UIView` has a `CALayer` for its `Layer` property. I want to exchange this against a `CAGradientLayer` as in the linked SO question. I want to use the `Colors` property but how should I do that in that context? I tried a few things but none of them worked. – testing Nov 10 '14 at 11:42

2 Answers2

3

This wonderful post (iOS Programming Recipe 20: Using CAGradientLayer In A Custom View) pointed me in the right direction:

using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreAnimation;
using MonoTouch.CoreGraphics;
using MonoTouch.ObjCRuntime;

namespace SampleApp
{
    public class GradientView : UIView
    {
//      public new CAGradientLayer Layer { get; private set; }
        private CAGradientLayer gradientLayer {
            get { return (CAGradientLayer)this.Layer; }
        }

        public GradientView ()
        {
        }


        [Export ("layerClass")]
        public static Class LayerClass ()
        {
            return new Class (typeof(CAGradientLayer));
        }

        public void setColors(CGColor[] colors){
            this.gradientLayer.Colors = colors;
        }

//      public override void Draw (RectangleF rect)
//      {
//          // Do nothing, the Layer will do all the drawing
//      }
    }
}

And here I create and set up my background view:

GradientView background = new GradientView ();
background.setColors (new CGColor[] {
    UIColor.FromRGB (18,200,45).CGColor,
    UIColor.White.CGColor,
    UIColor.White.CGColor
});

Now it seems to work. Using a custom property (inclusive cast) and a separate method did the trick. Of course you can write a full accessor. This was a simple test.

testing
  • 19,681
  • 50
  • 236
  • 417
1

You can just use like this. no need to create separate class for it.

 var gradient = new CAGradientLayer();
 gradient.Frame = new CoreGraphics.CGRect(0,0, width, height);
 gradient.Colors = new CoreGraphics.CGColor[] { UIColor.Clear.CGColor, 
                    UIColor.SystemBackgroundColor.CGColor };
  Layer.AddSublayer(gradient); 
Pratik Patel
  • 57
  • 2
  • 12