Carlos
Carlos

Reputation: 386

How to use CustomRenderer to render a different component

I have a sample Xamarin.Forms shared project where I have a MainPage.xaml. In order to achieve a material design look on iOS, I'm trying to use custom renderes to give the desired look to the componenets. Recently I found a few dependencies for Xamarin.iOS that can do that.

I would like to know what I need to do in order to use a CustomRenderer to render a compoment from an Xamarin.iOS dependency.

This is my iOS custom renderer for an entry:

[assembly: ExportRenderer (typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace MCTest.iOS
{
    public class CustomEntryRenderer : EntryRenderer
    {

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                // do whatever
            }
        }
    }
}

In here I'm able to customize the entry, but what really want to do is rendering a different compoment from a external (nuget) dependency. Is this possible ? If so, how can I achieve this ? Thanks.

Upvotes: 2

Views: 147

Answers (1)

Ax1le
Ax1le

Reputation: 6643

You can try to create a ContentPage in the PCL, Then make its renderer like:

[assembly: ExportRenderer(typeof(MaterialPage), typeof(MaterialPageRenderer))]
namespace SpecialPageRenderer.iOS
{
    public class MaterialPageRenderer : PageRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (Element != null)
            {
                var materialView = UIStoryboard.FromName("Main", null).InstantiateViewController("ViewController").View;

                NativeView.AddSubview(materialView);
            }
        }
    }   
}

Here I create a new MaterialViewController on iOS project called ViewController. Then I can construct it in this renderer, add its view on the NativeView. At last the dependency control will show.

I make a sample for you referring to: https://github.com/landl0526/SpecialMaterialPage

Upvotes: 2

Related Questions