SkiaSharp Canvas按钮不起作用

时间:2017-07-12 23:54:43

标签: xamarin xamarin.forms skiasharp

我有一个Xamrin Form网格布局,如下所示。有一个带有渐变色的SkiaSharp画布。我需要在画布中添加一个按钮来获得渐变背景。但是当我在画布中添加按钮时,它不会呈现任何内容(按钮x:名称=“ClickMe2”)。使用渐变背景渲染按钮的正确方法是什么?

XAML

<Grid x:Name="controlGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="200" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <views:SKCanvasView PaintSurface="OnSecondPainting" EnableTouchEvents="True" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" >
        <Button x:Name="ClickMe2" Text="ClickMe 2" HeightRequest="50"  VerticalOptions="Start" />
    </views:SKCanvasView>
    <Label Text="Bottom Left" Grid.Row="1" Grid.Column="0" />
    <Button x:Name="ClickMe1" Text="ClickMe 1" Grid.Row="1" Grid.Column="1" HeightRequest="50"  VerticalOptions="Start" />
</Grid>

C#

    private void OnSecondPainting(object sender, SKPaintSurfaceEventArgs e)
    {

        var surface = e.Surface;
        var canvas = surface.Canvas;
        canvas.Clear(SKColors.White);

        ///GRADIENT--------------------------------------------------------------------
        var colors = new SKColor[] { new SKColor(6, 107, 249), new SKColor(27, 162, 210), new SKColor(36, 182, 197) };
        var shader = SKShader.CreateLinearGradient(new SKPoint(300, 0), new SKPoint(300, 600), colors, null, SKShaderTileMode.Clamp);
        var paint = new SKPaint() { Shader = shader };
        canvas.DrawPaint(paint);


    }

1 个答案:

答案 0 :(得分:1)

这是因为画布视图实际上不是布局 - 它是fileObjects。这意味着它不支持儿童。

如果这确实是一个问题,那么随时在GitHub上打开一个问题: https://github.com/mono/skiasharp/issues我们可以在那里讨论这个问题,并从社区获取意见(例如更改或新视图)

如果您想在画布上放置视图,可以使用private getFileList() { this.fileObjects = []; // fileObjects defined as array this.fData.getFileList().subscribe((files) => { let _file:File = {}; // assign empty value to _file for(let i=0; i<files.length; i++) { let tmpArray:string[]; tmpArray = files[i].split('|'); _file.fileName = files[i]; _file.owner = tmpArray[0]; _file.brand = tmpArray[1]; _file.serial = tmpArray[2]; _file.flags = tmpArray[3]; this.fileObjects.push(_file); } }); } ViewGrid来执行此操作:

RelativeLayout

只要画布是第一个孩子,所有其他孩子都会出现在它上面。