实现BindingContextChanged事件时Xamarin.Forms奇怪的行为

时间:2017-12-03 13:25:24

标签: xamarin.forms binding-context

我有一个列表,其中包含单元格行最右侧的图标

当我实现BindingContextChanged来控制上下文操作时(根据特定条件启用和禁用)

<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;

        }

我发生了两个奇怪的行为

iOS平台图标中

将边距向左移动然后当我继续滚动它变得正常时 在 Android平台应用程序崩溃

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)

我试图解决问题分离监听器

theViewCell.BindingContextChanged -= FlightDetialCell_OnBindingContextChanged;

但这并不能解决我的问题

0 个答案:

没有答案