EventToCommandBehavior RelativeSource in Xamarin.Forms

44 views Asked by At

Why it is not work?

<Editor
    Text="{Binding OptionText}">
    <Editor.Behaviors>
        <xct:EventToCommandBehavior 
            EventName="Focused"
            Command="{Binding CheckEditorViewCommand,Source={RelativeSource AncestorType={x:Type view:SurveyViewModel}}}" />
    </Editor.Behaviors>
</Editor>  

I get crash app when I try open page with this xaml code.

01-14 07:50:43.611 F/mono-rt (28519): [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: Operation is not valid due to the current state of the object. 01-14 07:50:43.611 F/mono-rt (28519): at Xamarin.Forms.Binding.ApplyRelativeSourceBinding (Xamarin.Forms.BindableObject targetObject, Xamarin.Forms.BindableProperty targetProperty) [0x00041] in D:\a\1\s\Xamarin.Forms.Core\Binding.cs:158 01-14 07:50:43.611 F/mono-rt (28519): at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__7_0 (System.Object state) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021 01-14 07:50:43.611 F/mono-rt (28519): at Android.App.SyncContext+<>c__DisplayClass2_0.b__0 () [0x00000] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:36 01-14 07:50:43.611 F/mono-rt (28519): at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36 01-14 07:50:43.611 F/mono-rt (28519): at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-33/mcw/Java.Lang.IRunnable.cs:84 01-14 07:50:43.611 F/mono-rt (28519): at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V (_JniMarshal_PP_V callback, System.IntPtr jnienv, System.IntPtr klazz) [0x00005] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:22 01-14 07:50:43.611 F/mono-rt (28519): at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(intptr,intptr)

When I drop this is cod, page will work.
I have been understood that problem in binding way, but I don't understand how do it right.

1

There are 1 answers

0
Winteriscoming On

I resolved my problem with that binding approach:

<Editor
    Text="{Binding OptionText}">
        <Editor.Behaviors>
            <xct:EventToCommandBehavior
                EventName="TextChanged"
                CommandParameter="{Binding .}"
                Command="{Binding Source={x:Reference Name=SurveyPageCustomName},
                Path=BindingContext.CheckEditorViewCommand}" />
        </Editor.Behaviors>
</Editor>  

I don't why, but this is Command="{Binding CheckEditorViewCommand,Source={RelativeSource AncestorType={x:Type view:SurveyViewModel}}}" not work for behavior but work for TapGestureRecognizer. It is confused me.