Custom renderer only for one plattform?

FranciscoGGFranciscoGG ESMember ✭✭
edited September 2015 in Xamarin.Forms

Hello.

Since I am having performance problems with ListViews only in Android, I am working on a custom renderer, and writing natively the ItemView, with a fantastic result.

In iOS, I am not having these performance problems, so I would like to avoid to rewrite these custom renderer for iOS.

Is this possible to do?

Answers

  • JohnHairJohnHair GBMember ✭✭✭✭✭

    Yes, you register the custom renderer at the platform level. I do this for iOS specific enhancements that I dont want/need in Droid and vice versa.

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭

    Sure, you don't have to write your own renderer on every platform. The engine searches for the renderer of the type which is used. If it can't find one, then it searches for a renderer for the parent type and so on. So your custom ListView just has to inherit from Xamarin.Forms.ListView and it should work.

  • FranciscoGGFranciscoGG ESMember ✭✭
    edited September 2015

    @MichaelRumpler @JohnHair it seems there is some error, because I am getting an exception...here is my source code:

    XAML of the Cell:

            <controls:ItemCell Title="{Binding Name}" Description="{Binding Surname}">
              <Grid Style="{StaticResource ListViewCellGrid}">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="*"/>
                  <ColumnDefinition Width="auto"/>
                </Grid.ColumnDefinitions>
    
                <StackLayout Grid.Column="0">
                  <Label Text="{Binding Name}"/>
                  <Label Text="{Binding Surname}" />
                </StackLayout>
    
                <Image Grid.Column="2" Style="{StaticResource ListViewCellImage}"/>
            </Grid>
            </controls:ItemCell>
    

    ItemCell:
    public class ItemCell : ViewCell
    {
    public static readonly BindableProperty TitleProperty = BindableProperty.Create<ItemCell, string>(p => p.Title, default(string));
    public string Title
    {
    get { return (string)GetValue(TitleProperty); }
    set { SetValue(TitleProperty, value); }
    }

                    public static readonly BindableProperty DescriptionProperty = BindableProperty.Create<ItemCell, string>(p => p.Description, default(string));
                    public string Description
                    {
                        get { return (string)GetValue(DescriptionProperty); }
                        set { SetValue(DescriptionProperty, value); }
                    }
                }
    

    CustomRenderer:
    [assembly: ExportRenderer(typeof(ItemCell), typeof(ItemCellRenderer))]
    namespace Elfo.VisionMobile.Droid.Renderers
    {
    public class ItemCellRenderer : ViewCellRenderer
    {
    protected override Android.Views.View GetCellCore(Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context)
    {
    var itemCell = (ItemCell)item;

                            var view = convertView;
                            if (view == null)
                                view = (context as Activity).LayoutInflater.Inflate(Resource.Layout.NativeItemCell, null);
    
                            view.FindViewById<TextView>(Resource.Id.TitleText).Text = itemCell.Title;
                            view.FindViewById<TextView>(Resource.Id.DescriptionText).Text = itemCell.Description;
                            view.FindViewById<TextView>(Resource.Id.RelevantDataText).Text = itemCell.RelevantData;
    
                            return view;
                        }
                    }
                }
    

    I am getting a runtime exception (NullReferenceException).
    If I comment this block of code in XAML, it works:

              <Grid Style="{StaticResource ListViewCellGrid}">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="*"/>
                  <ColumnDefinition Width="auto"/>
                </Grid.ColumnDefinitions>
    
                <StackLayout Grid.Column="0">
                  <Label Text="{Binding Name}"/>
                  <Label Text="{Binding Surname}" />
                </StackLayout>
    
                <Image Grid.Column="2" Style="{StaticResource ListViewCellImage}"/>
            </Grid>
    
  • FranciscoGGFranciscoGG ESMember ✭✭
    edited September 2015

    Please help me :)

Sign In or Register to comment.