1

RoundCorner GridLayout を作成したいのですが、Xamarin で Gridlayot コントロールに角丸を追加しましたが、このように Gridlayout BackgroundColor が Back Side に表示されます。問題を解決するのを手伝ってください。 Drawable Add 後の GridLayout

Xamarin.Forms Xaml コード:

xmlns:local="clr-namespace:XamainControl.Renderer;assembly=XamainKcsControl"
<Grid BackgroundColor="Purple" WidthRequest="300">
         <Grid.RowDefinitions>
  <RowDefinition Height="Auto"/>      
</Grid.RowDefinitions>
<local:KcsGrid Grid.Row="0"
               BorderColor="Blue"
               BackgroundColor="Yellow"
               BorderRadius="20"
               BorderWidth="10" Padding="15" >
  <Label Text="Customization Page"
         FontSize="25"
         TextColor="Red"
         VerticalOptions="CenterAndExpand"
         HorizontalOptions="CenterAndExpand"/>
</local:KcsGrid>
</Grid>

カスタム グリッド プロパティ Xamarin.Forms の作成

 public class KcsGrid : Grid
{
    #region Constructor
    public KcsGrid() : base()
    {
        this.Margin = new Thickness(0, 0, 0, -6);
    }
    #endregion

    #region Properties   

    public static BindableProperty BorderColorProperty = BindableProperty.Create<KcsGrid, Color>(o => o.BorderColor, Color.Transparent);

    public Color BorderColor
    {
        get { return (Color)GetValue(BorderColorProperty); }
        set { SetValue(BorderColorProperty, value); }
    }

    public static BindableProperty BorderWidthProperty = BindableProperty.Create<KcsGrid, float>(o => o.BorderWidth, 0);

    public float BorderWidth
    {
        get { return (float)GetValue(BorderWidthProperty); }
        set { SetValue(BorderWidthProperty, value); }
    }

    public static BindableProperty BorderRadiusProperty = BindableProperty.Create<KcsGrid, float>(o => o.BorderRadius, 0);

    public float BorderRadius
    {
        get { return (float)GetValue(BorderRadiusProperty); }
        set { SetValue(BorderRadiusProperty, value); }
    }


    public static BindableProperty WidthValueProperty = BindableProperty.Create<KcsGrid, int>(o => o.WidthValue,100);

    public int WidthValue
    {
        get { return (int)GetValue(WidthValueProperty); }
        set { SetValue(WidthValueProperty, value); }
    }

    public static BindableProperty HeightValueProperty = BindableProperty.Create<KcsGrid, int>(o => o.HeightValue,50);

    public int HeightValue
    {
        get { return (int)GetValue(HeightValueProperty); }
        set { SetValue(HeightValueProperty, value); }
    }

    #endregion
}

Xamarin.Android コード:

名前空間 XamainKcsControl.Droid.Renderer {

public class CustomGridRender : ViewRenderer<KcsGrid,GridLayout>
{
    private BorderRenderer _renderer;
    GridLayout Cusgd;
    protected override void OnElementChanged(ElementChangedEventArgs<KcsGrid> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement != null || this.Element == null)
            return;                                     
        Cusgd = new GridLayout(Context);
        if (Control == null)
        {              
            var curgd = Element as KcsGrid;
            SetNativeControl(Cusgd);
            UpdateBackground(curgd);
            //UpdateMeasurelayout(curgd);
        }                               
    }    

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        if (Element == null)
            return;
        var Gd = Element as KcsGrid;

        if (e.PropertyName == KcsGrid.BorderColorProperty.PropertyName || e.PropertyName == KcsGrid.BorderWidthProperty.PropertyName || e.PropertyName == KcsGrid.BorderRadiusProperty.PropertyName)
        {
            UpdateBackground(Gd);
        }

        if (e.PropertyName == KcsGrid.WidthValueProperty.PropertyName || e.PropertyName == KcsGrid.HeightValueProperty.PropertyName)
        {
            UpdateMeasurelayout(Gd);
        }
        //SetNativeControl(Cusgd);
    }

    void UpdateMeasurelayout(KcsGrid Gd)
    {
        GridLayout.LayoutParams Gdparm = new GridLayout.LayoutParams();
        Gdparm.Width = (int)Forms.Context.ToPixels(Gd.WidthValue);
        Gdparm.Height = (int)Forms.Context.ToPixels(Gd.HeightValue);            
        Cusgd.LayoutParameters = Gdparm;
        //SetNativeControl(Cusgd);            
    }

    void UpdateBackground(KcsGrid Gd)
    {
        if (_renderer != null)
        {
            _renderer.Dispose();
            _renderer = null;
        }
        _renderer = new BorderRenderer();
        Cusgd.Background = _renderer.GetBorderBackground(Gd.BorderColor,Gd.BackgroundColor, Gd.BorderWidth, Gd.BorderRadius);            
    }
}

}

グラデーションドローアブル

  public Drawable GetBorderBackground(Color borderColor, Color backgroundColor, float borderWidth, float borderRadius)
    {
        if (_background != null)
        {
            _background.Dispose();
            _background = null;
        }
        borderWidth = borderWidth > 0 ? borderWidth : 0;
        borderRadius = borderRadius > 0 ? borderRadius : 0;
        borderColor = borderColor != Color.Default ? borderColor : Color.Transparent;
        backgroundColor = backgroundColor != Color.Default ? backgroundColor : Color.Transparent;

        var strokeWidth = Xamarin.Forms.Forms.Context.ToPixels(borderWidth);
        var radius = Xamarin.Forms.Forms.Context.ToPixels(borderRadius);
        _background = new GradientDrawable();
        _background.SetColor(backgroundColor.ToAndroid());
        if (radius > 0)
            _background.SetCornerRadius(radius);
        if (borderColor != Color.Transparent && strokeWidth > 0)
        {
            _background.SetStroke((int)strokeWidth, borderColor.ToAndroid());
        }

        return _background;
    }
4

0 に答える 0