2017-06-11 14 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> 

とのViewModelで:

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のコマンドを「{TapindingCommand}をバインドする」CommandParameter = "1"にしようとしましたが、コマンドをバインドする代わりに結果が同じ –

+0

となり、Tappedイベントハンドラを設定してみてください – Jason

答えて

1

問題が何であるかわかりました。 Source={x:Reference DetailsPage}は、xml名前空間定義のクラス名と競合していました。 メインのタグCarouselPageに適切なx:Nameを追加し、参照で同じ名前を使用すると、正常に動作しました。

関連する問題