C# 创建使用CAGradientLayer作为其层的UIView子类
我想转到C# 我不知道该如何转换:C# 创建使用CAGradientLayer作为其层的UIView子类,c#,ios,xamarin.ios,xamarin,cagradientlayer,C#,Ios,Xamarin.ios,Xamarin,Cagradientlayer,我想转到C# 我不知道该如何转换: @property (nonatomic, strong, readonly) CAGradientLayer *layer; 层是UIView的默认层。Xamarin已经有一个层属性。如何覆盖此内容?我需要覆盖它吗 我也试过了 public-CAGradientLayer层{[Export(“layer”)]get;[Export(“layer:)]set;} 但是如果我想设置图层的颜色,应用程序就会崩溃(System.Reflection.TargetI
@property (nonatomic, strong, readonly) CAGradientLayer *layer;
层
是UIView的默认层。Xamarin已经有一个层
属性。如何覆盖此内容?我需要覆盖它吗
我也试过了
public-CAGradientLayer层{[Export(“layer”)]get;[Export(“layer:)]set;}
但是如果我想设置图层的颜色,应用程序就会崩溃(System.Reflection.TargetInvocationException-NullReferenceException,当单元格出列时)。此外,它应该是只读的
然后我看到了它是如何被转换的:
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);
}
}
这对我没有帮助,因为我需要像SetFillColors这样的东西,这样我就可以使用CGColor[]
array。但没有这样的功能
如何用我的自定义CAGradientLayer
创建我的UIView
,使用C#和Monotouch?这篇精彩的文章()为我指明了正确的方向:
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
// }
}
}
在这里,我创建并设置了我的背景视图:
GradientView background = new GradientView ();
background.setColors (new CGColor[] {
UIColor.FromRGB (18,200,45).CGColor,
UIColor.White.CGColor,
UIColor.White.CGColor
});
现在它似乎起作用了。使用一个自定义属性(包括强制转换)和一个单独的方法完成了这个任务。当然,您可以编写完整的访问器。这是一个简单的测试。请详细解释,为什么这没有帮助?您想使用哪种SetFillColor方法,但无法使用?只有
SetFillColor
没有s。有不同的SetFillColor
方法可用。一个采用float[]组件
。另一个CGColor
。没有采用CGColor[]
的方法。我在任何地方都找不到setFillColor方法。显然,没有这样的方法。但是,由于您讨论的是CAGradientLayer,因此可以从中重写类,而不是CALayer。CAGradientLayer有一个颜色属性,它是CGColor[]。如果我猜对了你想做什么…谢谢你的回答。普通ui视图
的层
属性有一个CALayer
。我想用CAGradientLayer
来交换这个,就像在链接的SO问题中一样。我想使用Colors
属性,但是在这种情况下我应该怎么做呢?我试过几件事,但都不管用。