命令绑定在Xamarin Forms

时间:2017-03-02 18:36:25

标签: c# xaml xamarin.forms prism

我正在尝试将命令绑定到TapGesture到列表视图控件内的Frame。框架在列表视图的datatemplate中定义。

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

这里在viewmodel类中定义了LstSrc和GoToPageCommand ae。并且Item1在LstSrc中定义。

视图模型中定义的委托命令是

 public DelegateCommand<TappedEventArgs> GoToPageCommand => new DelegateCommand<TappedEventArgs>(
           async s => {                            
               await _navigationService.NavigateAsync("NextPage");              
           });

但是在点击时,控件不会进入命令方法。

1 个答案:

答案 0 :(得分:0)

此解决方案不使用绑定,但如果你采取不同的方法,它应该可行。

<Frame.GestureRecognizers>
      <TapGestureRecognizer
              Tapped="OnFrameClicked"
              NumberOfTapsRequired="1" />
</Frame.GestureRecognizers>

void async OnFrameClicked(object sender, EventArgs args)
{
    await _navigationService.NavigateAsync("NextPage"); 
}