画布视图不填充相对布局(XAML)

时间:2017-07-17 23:07:35

标签: xamarin xamarin.forms relativelayout

我在相对布局(Xamarin Forms项目)中有一个SkiaSharp画布视图。当我单独使用画布时,它会填满整个页面。但是当我在相对布局中使用它时,它不会填充相对布局的整个空间。如何使画布填充整个相对布局?

enter image description here

XAML

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyHomeScreen2"
             xmlns:views="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"
             x:Class="MyHomeScreen2.MainPage">

    <RelativeLayout BackgroundColor="Yellow">

        <views:SKCanvasView PaintSurface="OnSecondPainting" EnableTouchEvents="True" x:Name="MyCanvas"

              RelativeLayout.XConstraint=
                 "{ConstraintExpression Type=RelativeToParent, 
                                        Property=X,
                                        Constant=0}"

             RelativeLayout.YConstraint=
                 "{ConstraintExpression Type=RelativeToParent,
                                        Property=Y, 
                                        Constant=0}">
        </views:SKCanvasView>

        <BoxView Color="Blue"
            WidthRequest="100" 
            HeightRequest="50"
           RelativeLayout.XConstraint=
                 "{ConstraintExpression Type=RelativeToView, 
                                        Property=Width,
                                        ElementName=MyCanvas,
                                        Factor=0.5}"

             RelativeLayout.YConstraint=
                 "{ConstraintExpression Type=RelativeToView,
                                        Property=Y, 
                                        ElementName=MyCanvas,
                                        Constant=25}" />


    </RelativeLayout>
</ContentPage>

1 个答案:

答案 0 :(得分:3)

Canvas指定X和Y约束,但不指定宽度和高度。它们应该相对于父系设置为因子1

<强> XAML

    <views:SKCanvasView PaintSurface="OnSecondPainting" EnableTouchEvents="True" x:Name="MyCanvas"

          RelativeLayout.XConstraint=
             "{ConstraintExpression Type=RelativeToParent, 
                                    Property=X,
                                    Constant=0}"

         RelativeLayout.YConstraint=
             "{ConstraintExpression Type=RelativeToParent,
                                    Property=Y, 
                                    Constant=0}"
         RelativeLayout.HeightConstraint = 
                    "{ConstraintExpression Type=RelativeToParent,
                                           Property=Height,
                                           Factor=1}"

         RelativeLayout.WidthConstraint = 
                    "{ConstraintExpression Type=RelativeToParent,
                                           Property=Width,
                                           Factor=1}"                            

                        >
    </views:SKCanvasView>