【发布时间】:2017-07-22 15:26:37
【问题描述】:
我正在尝试将 TapGesture 的命令绑定到列表视图控件内的框架。框架在列表视图的数据模板中定义。
<ListView x:Name="listView"
ItemsSource="{Binding LstSrc}"
RowHeight="75" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="0"
Padding="0"
BackgroundColor="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Frame Grid.Column="0" BindingContext="{Binding Item1}"
Margin="0" Padding="0" x:Name="f1">
<Frame.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BindingContext.GoToPageCommand, Source={x:Reference this}}"
CommandParameter="{Binding BindingContext.Id, Source={x:Reference f1}}"/>
</Frame.GestureRecognizers>
</Frame>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这里 LstSrc 和 GoToPageCommand ae 定义在 viewmodel 类中。而 Item1 是在 LstSrc 中定义的。
在视图模型中定义的委托命令是
public DelegateCommand<TappedEventArgs> GoToPageCommand => new DelegateCommand<TappedEventArgs>(
async s => {
await _navigationService.NavigateAsync("NextPage");
});
但是在点击时,控件不会进入命令方法。
【问题讨论】:
-
您不需要在绑定中指定 BindingContext,如果您查看调试输出窗口,您可能会看到绑定错误。尝试在绑定路径中不使用 BindingContext 绑定到 GoToPageCommand。
标签: c# xaml xamarin.forms prism