Xamarin.Forms Strange Behavior when Implementing BindingContextChanged Event

孤者浪人 提交于 2019-12-13 18:48:17

问题


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

来源:https://stackoverflow.com/questions/47618834/xamarin-forms-strange-behavior-when-implementing-bindingcontextchanged-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!