2017-06-11 60 views
0

我試圖在圖像中綁定TapGestureRecognizer(如圖所示here),但ViewModel中的相關Icommand不會被觸發。我正在使用lightMVVM框架。Xamarin表單圖像TapGestureRecognizer命令不叫

這裏是我的代碼:

<?xml version="1.0" encoding="utf-8" ?> 
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms" 
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
       x:Class="ZoccoloManager.Views.DetailsPage" 
       ItemsSource="{Binding Cows}"> 
    <CarouselPage.ItemTemplate> 
     <DataTemplate> 
      <ContentPage x:Name="DetailsPage"> 
       <RelativeLayout> 
        <Image x:Name="fl_left" 
          Source="zoccolo_leftside.png" 
          RelativeLayout.WidthConstraint= 
          "{ConstraintExpression Type=RelativeToParent, 
                Property=Width, 
                Factor=0.3 }" 
          RelativeLayout.HeightConstraint= 
          "{ConstraintExpression Type=RelativeToParent, 
                Property=Height, 
                Factor=0.3 }" 
          RelativeLayout.XConstraint = 
          "{ConstraintExpression Type=RelativeToParent, 
                Property=Width, 
                Factor=0.07}" 
          RelativeLayout.YConstraint = 
          "{ConstraintExpression Type=RelativeToParent, 
                Property=Height, 
                Factor=0.07}"> 
         <Image.GestureRecognizers> 
          <TapGestureRecognizer Command="{Binding Path=BindingContext.TapImageCommand, Source={x:Reference DetailsPage}}" /> 
         </Image.GestureRecognizers> 
        </Image> 
       </RelativeLayout> 
      </ContentPage> 
     </DataTemplate> 
    </CarouselPage.ItemTemplate> 
</CarouselPage> 

,並在視圖模型:

public class DetailsViewModel : ViewModelBase 
{ 
    private readonly INavigationService _navigationService; 
    private IRepository _repository; 
    public ICommand TapImageCommand { get; private set; } 
    public ObservableCollection<Cow> Cows { get; set; } 

    public DetailsViewModel(INavigationService navigationService) 
    { 
     _repository = ServiceLocator.Current.GetInstance<IRepository>(); 
     Debug.WriteLine(DateTime.Now + ": Calling LoadCows"); 
     LoadCows(); 
     Debug.WriteLine(DateTime.Now + ": Called LoadCows"); 
     _navigationService = navigationService; 

     TapImageCommand = new Command(OpenPopup); 
    } 

    private async Task LoadCows() 
    { 
     Debug.WriteLine(DateTime.Now + ": Started execution LoadCows"); 
     await Task.Run(() => 
     { 
      Cows = new ObservableCollection<Cow>(); 
      foreach (var cow in _repository.GetCompany(0).Cows) 
      { 
       Cows.Add(cow); 
      } 
     }); 
     Debug.WriteLine(DateTime.Now + ": Finished execution LoadCows"); 
    } 

    private void OpenPopup() 
    { 
     Debug.WriteLine("Opening popup "); 
    } 
} 

一切負載罰款,但調試它的ICommand TapImageCommand的吸氣劑不會被調用。綁定到ObservableCollection工作正常,我有正確數量的頁面作爲列表中的元素。

我在想什麼?

+0

檢查你的綁定表達式 – Jason

+0

你能解釋一下嗎?我也嘗試將TapGestureRecognizer的命令設爲「{Binding TapImageCommand}」CommandParameter =「1」,但結果與 –

+0

相同,而不是綁定Command,只需設置Tapped事件處理程序 – Jason

回答

1

好吧,我已經找出了問題所在。 Source={x:Reference DetailsPage}與xml名稱空間定義中的類名衝突。 在主標籤CarouselPage中添加一個合適的x:Name,並在參考中使用相同的名稱,使其工作得很好。