<Entry.Behaviors> 在 ListView ItemTemplate 中不起作用

<Entry.Behaviors> doesn't work inside ListView ItemTemplate

我在 ListView 的 ItemTemplate 中使用一个 Entry 视图,我想将一个命令绑定到 Entry 视图的 Completed 事件(ala MVVM)。 带有 DataTemplate 定义的 XAML 的顶部如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:models="clr-namespace:MedLemnMobile.Models"
             xmlns:viewModel="clr-namespace:MedLemnMobile.ViewModels"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="MedLemnMobile.Views.EquationLibPage"
             Title="EquationSets"
             Focused="ContentPage_Focused"
             x:FieldModifier="public"
             x:Name="myEqLibPage"
             AutomationId="myEqLibPage"
             BackgroundColor="{DynamicResource PageBackgroundColor}"
             x:DataType="viewModel:EquationLibViewModel">

    <ContentPage.BindingContext>
        <viewModel:EquationLibViewModel/>
    </ContentPage.BindingContext>

    <ContentPage.Resources>
        <DataTemplate x:Key="myEqSetDataTemplateViewCell_1">
            <ViewCell>
                <ViewCell.View Background="Goldenrod">
                    <StackLayout>
                        <Entry x:FieldModifier="public" x:Name="myEquationSetNameEntry"
                               x:DataType="models:EquationSet"
                               Text="{Binding EquationSetName, Mode=TwoWay}"
                               TextColor="{DynamicResource TextForegroundColor}">
                            <Entry.Behaviors>
                                <xct:EventToCommandBehavior
                                    x:DataType="viewModel:EquationLibViewModel"
                                    EventName="Completed"
                                    Command="{Binding EquationSetNameCompletedCommand}"
                                    CommandParameter="{x:Reference myEqLibPage}"/>
                            </Entry.Behaviors>
                        </Entry>
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>

稍后在 XAML 中,ListView 定义如下所示:

        <Grid Grid.Row="1" Grid.Column="0">
            <ListView   x:FieldModifier="public" x:Name="myEquationSetsListView"
                        AutomationId="myEquationSetsListView"
                        ItemsSource="{Binding EquationLib}"
                        SelectionMode="Single"
                        ItemTemplate="{StaticResource myEqSetDataTemplateViewCell_1}">
            </ListView>
        </Grid>

并且,ViewModel 中的命令声明如下所示:

namespace MedLemnMobile.ViewModels
{
    public class EquationLibViewModel : ViewModelBase
    {
        public ICommand GotoMenuCommand { get; }
        public ICommand GotoParamsCommand { get; }
        public ICommand EquationSetNameCompletedCommand { get; }

        public EquationLibViewModel()
        {
            GotoMenuCommand = new Command<EquationLibPage>(GotoMenu);
            GotoParamsCommand = new Command<EquationLibPage>(GotoParams);
            EquationSetNameCompletedCommand = new Command<EquationLibPage>(EquationSetNameCompleted);
        }

我希望当用户“完成”在条目视图中输入值时,命令 EquationSetNameCompletedCommand 将触发。 我在命令代码的开头设置了一个断点,并且该断点永远不会触发。 任何帮助弄清楚为什么这个命令没有触发的帮助将不胜感激。

我成功了!答案在命令绑定中如下:

                    <Entry.Behaviors>
                        <xct:EventToCommandBehavior
                            x:DataType="viewModel:EquationLibViewModel"
                            EventName="Completed"
                            Command="{Binding Path=BindingContext.EquationSetNameCompletedCommand,
                                      Source={Reference myEqLibPage}}"
                            CommandParameter="{Reference myEqLibPage}"/>
                    </Entry.Behaviors>