Xamarin.Forms 中的EventToCommandBehaviorrelativeSource

问题描述 投票:0回答:1

为什么不起作用?

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

当我尝试使用此 xaml 代码打开页面时,应用程序崩溃。

01-14 07:50:43.611 F/mono-rt (28519): [错误] 致命未处理 异常:System.InvalidOperationException:操作无效 由于对象的当前状态。 01-14 07:50:43.611 F/单声道 (28519):在 Xamarin.Forms.Binding.ApplyRelativeSourceBinding (Xamarin.Forms.BindableObject 目标对象, Xamarin.Forms.BindableProperty targetProperty) [0x00041] 在 D: \s\Xamarin.Forms.Core\Binding.cs:158 01-14 07:50:43.611 F/mono-rt (28519):位于 System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__7_0 (System.Object 状态) [0x00000] 中 /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): 在 Android.App.SyncContext+<>c__DisplayClass2_0.b__0 () [0x00000] 在 /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): 在 Java.Lang.Thread+RunnableImplementor.Run()[0x00008]中 /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): 在 Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] 中 /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): 在 Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V (_JniMarshal_PP_V 回调、System.IntPtr jnienv、System.IntPtr klazz) [0x00005] 在 /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 (包装器本机到托管) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(intptr,intptr)

当我放下这是鳕鱼时,页面就会工作。
我已经以绑定方式理解了这个问题,但我不明白如何正确执行。

c# xamarin xamarin.forms mono
1个回答
0
投票

我用这种绑定方法解决了我的问题:

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

我不知道为什么,但这

Command="{Binding CheckEditorViewCommand,Source={RelativeSource AncestorType={x:Type view:SurveyViewModel}}}"
不适用于行为,但适用于
TapGestureRecognizer
。这让我很困惑。

© www.soinside.com 2019 - 2024. All rights reserved.