问题描述
我在 ListView 的 ItemTemplate 中使用 Entry 视图,并且我想将 Command 绑定到 Entry 视图的 Completed 事件(ala MVVM)。 带有 DataTemplate 定义的 XAML 的顶部如下所示:
ExecStart=/bin/bash -lc 'RAILS_ENV=production SERVER_TYPE=111 bundle exec puma -C /home/ubuntu/rails_current_server/config/puma.rb'
稍后在 XAML 中,ListView 定义如下所示:
<?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>
而且,viewmodel 中的 Command 声明如下所示:
<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>
我希望当用户在 Entry 视图中“完成”输入值时,Command EquationSetNameCompletedCommand 将触发。 我在命令代码的开头设置了一个断点,断点永远不会被触发。 任何帮助弄清楚为什么此命令没有触发的帮助将不胜感激。
解决方法
我开始工作了!答案在命令绑定中,如下所示:
<Entry.Behaviors>
<xct:EventToCommandBehavior
x:DataType="viewModel:EquationLibViewModel"
EventName="Completed"
Command="{Binding Path=BindingContext.EquationSetNameCompletedCommand,Source={Reference myEqLibPage}}"
CommandParameter="{Reference myEqLibPage}"/>
</Entry.Behaviors>