Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Creating simple control with custom renderer

$
0
0

Hi.

I'm starting a project with Xamarin.Forms an cannot create the most basic custom rendered control with child controls, I always get a "SetElement did not create the correct number of children".

This is my control inherited from StackLayout:


public class ItemWidget : StackLayout
{
        public ItemWidget (){

        }

}

This is the page where I'm using it:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XXXXXX;assembly=XXXXXX" x:Class="XXXXX.LoginPage">
    <ContentPage.Content>
            <local:ItemWidget VerticalOptions="Center" HorizontalOptions="Center" WidthRequest="200" HeightRequest="200">
                    <Label Text="caca" />
            </local:ItemWidget>
    </ContentPage.Content>

And this is the renderer:


        [assembly: ExportRenderer (typeof (ItemWidget), typeof (GradientBoxRenderer))]
        namespace XXXXXXX
        {

            public class GradientBoxRenderer : ViewRenderer<ItemWidget, LinearLayout> 
        {

                protected override void OnElementChanged (ElementChangedEventArgs<ItemWidget> e)
                {

                    var layt = new LinearLayout (Context);

                    GradientDrawable grad = new GradientDrawable ();
                    grad.SetOrientation (GradientDrawable.Orientation.TlBr);
                    grad.SetColors (new int[]{ Android.Graphics.Color.AliceBlue.ToArgb(), Android.Graphics.Color.BlueViolet.ToArgb() });
                    layt.Background = grad;
                    SetNativeControl (layt);

                    base.OnElementChanged (e);

                }
            }

}

What I'm missing?

Is not possible to create a custom control with child controls?


Viewing all articles
Browse latest Browse all 75885

Trending Articles