Xamarin.Forms Strange Behavior when Implementing BindingContextChanged Event

625 views Asked by At

I have a list that has icon to most right of cell row

when I implementing BindingContextChanged to control context action (enable and disable according to certain condition )

<customRender:ExtViewCell  BindingContextChanged="FlightDetialCell_OnBindingContextChanged">


private void FlightDetialCell_OnBindingContextChanged(object sender, EventArgs e)
        {

            var deleteAction = new MenuItem { Text="Delete" , IsDestructive=true};
            deleteAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
            deleteAction.Clicked +=  (sender2, e2) =>
            {
                DeleteMenuItem_OnClicked(sender2, e2);
            };

                ViewCell theViewCell = ((ViewCell)sender);
                var item = theViewCell?.BindingContext as DocumentsListItem;

                if (item != null)
                {
                    if (item.Title !="certain title" )
                    {
                    theViewCell.ContextActions.Add(deleteAction);
                    }

                }
               // theViewCell.BindingContextChanged -= FlightDetialCell_OnBindingContextChanged;

        }

I has two strange behavior happened

in iOS platform icon take margin to left then when I keep scroll it become normal in Android platform app crashed

Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference

 --- End of managed Java.Lang.NullPointerException stack trace ---
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
    at android.widget.AdapterView.getPositionForView(AdapterView.java:602)
    at md5b60ffeb829f638581ab2bb9b1a7f4f3f.ViewCellRenderer_ViewCellContainer_LongPressGestureListener.n_onLongPress(Native Method)
    at md5b60ffeb829f638581ab2bb9b1a7f4f3f.ViewCellRenderer_ViewCellContainer_LongPressGestureListener.onLongPress(ViewCellRenderer_ViewCellContainer_LongPressGestureListener.java:51)
    at android.view.GestureDetector.dispatchLongPress(GestureDetector.java:690)
    at android.view.GestureDetector.access$200(GestureDetector.java:37)
    at android.view.GestureDetector$GestureHandler.handleMessage(GestureDetector.java:266)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

my attempts to fix the issue detach listener

theViewCell.BindingContextChanged -= FlightDetialCell_OnBindingContextChanged;

but this don't solve my problem

0

There are 0 answers