Issue with Custom Renderer in Xamarin Forms

I have a custom control defined and used twice on a same xaml page in PCL project, both with different ItemSource, but whenever the page load's both the components display's the same data eventhough the Item source is different.. Did anyone faced this issue?

OS: Android

For eg:
Component 1 and Component 2 belongs to CustomRenderer1 type
Component 1 is binded with ItemSource1
Component 2 is binded with ItemSource2

But when the Components are displayed in the UI both displays the same data which is of ItemSource2.

Answers

  • DeepakRavisankarDeepakRavisankar USMember

    Found that Problem is with the OnElementPropertyChanged whenever it executes when ItemSource change it updates all the components. Is there a way we can specify which component should update ?

  • MikeDennisMikeDennis USMember ✭✭

    @AdamP any idea on this? even I am kinda struck on the same issue

  • JohnHardmanJohnHardman GBUniversity mod

    @DeepakRavisankar @MikeDennis -

    Does the sender argument of OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) provide you with enough information to decide how to handle the property changed?

  • MikeDennisMikeDennis USMember ✭✭

    @JohnHardman I added 2 components on my PCL project which points to the same custom renderer with different data set but when it loaded on the UI both shows same data.

    this is my customRenderer:
    protected override void OnElementChanged(ElementChangedEventArgs e)
    {

            base.OnElementChanged(e);
    
            var view = View = e.NewElement as PickerControl;
    
            var data =view.ItemsSource as List<string>;
            var i = 0;
            var dataString = new String[data.Count];
            foreach (var d in data)
            {
                dataString[i] =new String(d);
                i++;
            }
            DisplayCircleData(dataString);
    
        }
    
    
        private void DisplayCircleData(String[] dataString)
        {
    
            var inflator = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
            var layout = inflator.Inflate(Resource.Layout.Main, null);
            var circleD= layout.FindViewById<CircleData>(Resource.Id.CircleData);
    
            circleD.Cyclic = false;
            circleD.ViewAdapter = new PickerCellAdapter(Application.Context, dataString);
    
            var cd= layout.FindViewById(Resource.Id.CircleData);
            ((ViewGroup)circleD.Parent).RemoveView(cd);
            SetNativeControl(circleD);
    
        }
    
  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @MikeDennis - have a look at OnElementChanged usage in Basics of Custom Renderers to ensure you handle each call of the OnElementChanged property first.

    Second if the data sources end up the same the simple issue is the view.ItemsSource is referencing the same object and hence update each other. A custom render will be called for each element that it renders but won't mix up elements.

  • @JohnHardman , @AdamP and @MikeDennis thanks a lot for your help on this issue, I have a question, if i have same element repeated twice on my .axml, and if i use LayoutInflator to identify the element and pass different data source to both the elements will it show the same data or different data in the custom renderer?

    For eg:
    Xaml has
    1. Element 1 of type T1
    2. Element 2 of type T1

    On my custom renderer if I pass data source(ds1) to E1 and (ds2) to E2

    will it show DS2 on both or it will show DS1 in E1 and DS2 in E2?

Sign In or Register to comment.