首页 > 解决方案 > SkiaSharp 矩形的大小比 iOS 上的小

问题描述

我用 SkiaSharp 绘制位置 x、y、宽度和高度的列表项。这在 Android 中没问题,但在 iOS 中布局和大小项目较小。请查看以下代码和图像以了解:

在 Xaml 中:

    <ContentPage.Content>
        <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand" BackgroundColor="White">
            <Grid Padding="20">
                <skia:SKCanvasView x:Name="canvasView" 
                               PaintSurface="SKCanvasView_PaintSurface" BackgroundColor="Beige"/>
            </Grid>
        </StackLayout>
    </ContentPage.Content>

在视图中:

    private void SKCanvasView_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e)
    {
        try
        {
            int NumberColums = 10;
            int NumberRows = 9;
            Size m_CurrentTableSize = new Size();
    
            canvasView.WidthRequest = this.Width; //width of page
            canvasView.HeightRequest = this.Height;//height of page
    
            var canvas = e.Surface.Canvas;
            var widthView = Width;
            var heightView = Height;
            m_CurrentTableSize = new Size(widthView / NumberColums, heightView / NumberRows);
    
            ObservableCollection<Table> Tables = _viewModel.Tables;
    
            for (int i = 0; i < Tables.Count; i++)
            {
                Table tableModel = Tables[i];
    
                int row = tableModel.PositionY;
                int col = tableModel.PositionX;
    
                int height = tableModel.Height;
                int width = tableModel.Width;
    
                var dH = (float)m_CurrentTableSize.Height / 68;
                var dW = (float)m_CurrentTableSize.Width / 68;
    
                row = (int)(dH * (float)row);
                col = (int)(dW * (float)col);
    
                height = (int)(height * dH);
                width = (int)(width * dW);
    
                var minSize = Math.Min(height, width);
    
                tableModel.Height = (int)minSize;
                tableModel.Width = (int)minSize;
    
                canvas.DrawRoundRect(new SKRoundRect(new SKRect((float)col, (float)row, (float)col + (float)tableModel.Height, (float)row + (float)tableModel.Width)), GetColor(tableModel.Status));
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
    }

在Android显示正常:

在此处输入图像描述

在 iOS 中,布局不完整:

在此处输入图像描述

感谢您的意见。

标签: xamarin.formsskiasharp

解决方案


尝试为网格设置 FillAndExpand。像这样

<Grid Padding="20" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

推荐阅读