Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将渐变设置为StyledStringElement_C#_Xamarin.ios_Monotouch.dialog - Fatal编程技术网

C# 将渐变设置为StyledStringElement

C# 将渐变设置为StyledStringElement,c#,xamarin.ios,monotouch.dialog,C#,Xamarin.ios,Monotouch.dialog,我正在尝试使用CAGradientLayer为StyledStringElement中的单元格设置渐变。这是我目前掌握的代码: public class DerreckStyledStringElement : StyledStringElement { public DerreckStyledStringElement(string caption) : base(caption) { } public override UITableViewCell GetCell

我正在尝试使用
CAGradientLayer
StyledStringElement
中的单元格设置渐变。这是我目前掌握的代码:

public class DerreckStyledStringElement : StyledStringElement
{
    public DerreckStyledStringElement(string caption) : base(caption) {
    }

    public override UITableViewCell GetCell(UITableView tv)
    {
        var x = base.GetCell(tv);
        CAGradientLayer g = new CAGradientLayer();
        g.Frame = x.Frame;
        g.MasksToBounds = true;
        x.ClipsToBounds = true;
        g.Colors = new MonoTouch.CoreGraphics.CGColor[]
        {
            new UIColor(0f, 153f, 235f, 255f).CGColor,
            new UIColor(0f, 197f, 244f, 255f).CGColor
        };

        UIView bg = new UIView(x.Bounds);
        bg.TranslatesAutoresizingMaskIntoConstraints = true;
        bg.Layer.InsertSublayer(g, 0);
        x.BackgroundView = bg;

        this.TextColor = UIColor.White;
        return x;
    }

}
我正在测试的这个按钮,它本身就是一个正方形,没有圆角,右边延伸到设备的右边缘。最有可能的是,它可能会延伸到离屏幕稍远的地方,因为按钮有一个看起来正确的左边距

What it should look like
|  /---------------------\  |
|  |    BUTTON TEXT      |  |
|  \---------------------/  |

What it looks like
|  |------------------------|
|  |    BUTTON TEXT         |
|  |------------------------|
我使用
g.MasksToBounds=true的地方
x.ClipsToBounds=true,且
bg.translatesAutoResizingMackintoConstraints=true尝试将背景剪辑到按钮框本身

因为我没有修改SelectedBackground,所以当点击它时,它确实会在周围显示正确的圆角矩形框

此外,颜色看起来正确,但渐变看起来拉伸,因此按钮看起来像渐变中的一种颜色,而不是渐变本身


我做错了什么?

单个单元格负责使用圆角渲染自己


有几种方法,一种是在渲染期间执行(请参见ImageElement以获取示例)。另外,如果您选择沿层路径向下,则要在层上设置CornerRadius属性。

谢谢,我将查看ImageElement并查看它是如何在那里完成的。除此之外,还有没有其他方法不使用图像或图层就可以做到这一点?我看到我只是在顶部添加了一个图层,它根本不考虑按钮的边界,我觉得图层路线可能不是这里要走的路。