0

I have followed the video on creating a custom render for xamarin forms on: https://www.youtube.com/watch?v=ux09gAB13kQ (thanks to Houssem Dellai)....the code is as follows:

In The main project solution add a class

public class RoundedEntry : Entry
{
}

in the android solution add:

 [assembly: ExportRenderer(typeof(RoundedEntry), typeof(RoundedEntryRendererAndroid))]
namespace Project.Droid
{
    public class RoundedEntryRendererAndroid : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if(e.OldElement == null)
            {
                //Use this code if want to use button from XAML page
               // Control.SetBackgroundResource(Resource.Layout.RoundedShape);

                
                //Use this button if you want to create button from c#
            var gradientDrawable = new GradientDrawable();
            gradientDrawable.SetCornerRadius(60f);
            gradientDrawable.SetStroke(5, Android.Graphics.Color.DeepPink);
            gradientDrawable.SetColor(Android.Graphics.Color.LightGray);
            Control.SetBackground(gradientDrawable);

            Control.SetPadding(50, Control.PaddingTop, Control.PaddingRight,
               Control.PaddingBottom);

            }
        }

        public RoundedEntryRendererAndroid()
           : base(null)
        {
            // Default constructor needed for Xamarin Forms bug?
            throw new Exception("This constructor should not actually ever be used");
        }
    }
}

and in the IOS add:

  [assembly: ExportRenderer(typeof(RoundedEntry), typeof(RoundedEntryRendererIos))]
namespace Project.iOS
{
    public class RoundedEntryRendererIos : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if(e.OldElement == null)
            {
                Control.Layer.CornerRadius = 20;
                Control.Layer.BorderWidth = 3f;
                Control.Layer.BorderColor = Color.DeepPink.ToCGColor();
                Control.Layer.BackgroundColor = Color.LightGray.ToCGColor();

                Control.LeftView = new UIKit.UIView(new CGRect(0, 0, 10, 0));
                Control.LeftViewMode = UIKit.UITextFieldViewMode.Always;
            }
        }
    }
}

Then when button is pressed load testPage with the custom render:

 <Button Text="Order" Clicked="ProceedToCheckout" HorizontalOptions="End"></Button>

void ProceedToCheckout(object sender, EventArgs e)
        {
              Application.Current.MainPage = new NavigationPage(new TestPage());
        }


<?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:Lagans"
             x:Class="Lagans.Views.TestPage">
    
        <local:RoundedEntry></local:RoundedEntry>


</ContentPage>

namespace Projects.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TestPage : ContentPage
    {
        public TestPage ()
        {
            InitializeComponent ();
        }
    }
}

However when testpage loads the project crashes with the error: the application is in break mode.

public TestPage ()
        {
            InitializeComponent ();
        }

is hit with a break point. but then it crashes

I am running the project on the android project.

does anyone have any idea what I am doing wrong? thank you

John
  • 3,965
  • 21
  • 77
  • 163

2 Answers2

0

I just use your code and everything works well on my side. I test it in both Android and iOS side.

I uploaded my sample project here and you can check it.

Please feel free to ask me any question if you have.

Here is the result:

enter image description here

nevermore
  • 15,432
  • 1
  • 12
  • 30
0

I think you need to define a parent layout in Content Page. May be a <Stacklayout> can work well.

<?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:Lagans"
             x:Class="Lagans.Views.TestPage">
    <Stacklayout>
        <local:RoundedEntry></local:RoundedEntry>
    </Stacklayout>

</ContentPage>
Inshal Irshad
  • 227
  • 3
  • 17