2017-10-10 58 views
1

在我的XAML我:Xamarin.Forms點擊Gesturer沒有響應

<Label Text="{Binding DisplayName}" TextColor="Blue"> 
    <Label.GestureRecognizers> 
     <TapGestureRecognizer Command="{Binding TapCommand}" 
          CommandParameter="{Binding URL}" /> 
    </Label.GestureRecognizers> 
</Label> 

在視圖模型(我已檢查結合上下文)我有

private ICommand tapCommand; 
public ICommand TapCommand 
{ 
    get { return tapCommand; } 
} 

public LinkReportPageViewModel() 
{ 
    PopulateLinkReport(); 
    tapCommand = new Command(OnTapped); 
} 

public void OnTapped(object tappedURL) 
{ 
    Log.LogThis($"Tapped on {tappedURL}", "LinkReportPageVM", DateTime.Now); 
} 

我想到的是,當我點擊標籤,我會在OnTapped中找到一個斷點,但我沒有。我錯過了什麼嗎?

請注意,此標籤位於分組列表中。

這是基於https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/gestures/tap/

謝謝!

+0

您的標籤是嵌套stackLayout嗎? –

+0

你的代碼看起來很好,但真正重要的是你有Command屬性的上下文。如果標籤位於列表中,則每個項目的BindingContext是列表項本身,而不是父上下文(綁定上下文默認從父項繼承)。請發佈你的ListView xaml代碼(只對你的問題重要),我會給你一個適當的答案=) –

回答

2

正如Diego提到的,標籤的綁定上下文是ListView綁定到的數據項的模型。嘗試將其更改爲這樣:

<Label Text="{Binding DisplayName}" TextColor="Blue"> 
    <Label.GestureRecognizers> 
     <TapGestureRecognizer Command="{Binding Path=BindingContext.TapCommand, Source={x:Reference NameOfListView}}" 
          CommandParameter="{Binding URL}" /> 
    </Label.GestureRecognizers> 
</Label> 

還要確保你給你的ListView的X:名稱

我們說的是命令,它的結合上下文是家長的ListView,這勢必會包含視圖模型您命令。