新增點選手勢辨識器

Download Sample 下載範例

點選手勢可用於點選偵測,並使用 TapGestureRecognizer 類別實作。

若要讓使用者介面項目可透過點選手勢點選,請建立 TapGestureRecognizer 執行個體,處理 Tapped 事件,然後將新的手勢辨識器新增至使用者介面項目的 GestureRecognizers 集合。 下列程式碼範例顯示附加至 Image 項目的 TapGestureRecognizer

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
    // handle the tap
};
image.GestureRecognizers.Add(tapGestureRecognizer);

根據預設,影像會回應單一點選。 將 NumberOfTapsRequired 屬性設定為等候點兩下或更多下 (如有需要)。

tapGestureRecognizer.NumberOfTapsRequired = 2; // double-tap

NumberOfTapsRequired 設定為 1 以上時,只有在固定期間內 (無法設定此期間) 發生點選次數時,才會執行事件處理常式。 如果在該期間內未點第二下 (或後續幾下),則會有效忽略並重新開始「點選計數」。

使用 XAML

您可以在 XAML 中使用附加屬性將手勢辨識器新增至控制項。 以下顯示將 TapGestureRecognizer 新增至影像的語法 (在本案例中定義「點兩下」事件):

<Image Source="tapped.jpg">
    <Image.GestureRecognizers>
        <TapGestureRecognizer
                Tapped="OnTapGestureRecognizerTapped"
                NumberOfTapsRequired="2" />
  </Image.GestureRecognizers>
</Image>

事件處理程式的程式代碼會遞增計數器,並將影像從色彩變更為黑白。

void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{
    tapCount++;
    var imageSender = (Image)sender;
    // watch the monkey go from color to black&white!
    if (tapCount % 2 == 0) {
        imageSender.Source = "tapped.jpg";
    } else {
        imageSender.Source = "tapped_bw.jpg";
    }
}

使用 ICommand

使用 Model-View-ViewModel (MVVM) 模式的應用程式通常會使用 ICommand,而不是直接連接到事件處理常式。 TapGestureRecognizer 可以透過在程式碼中設定繫結,輕鬆地支援 ICommand

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.SetBinding (TapGestureRecognizer.CommandProperty, "TapCommand");
image.GestureRecognizers.Add(tapGestureRecognizer);

或使用 XAML:

<Image Source="tapped.jpg">
    <Image.GestureRecognizers>
        <TapGestureRecognizer
            Command="{Binding TapCommand}"
            CommandParameter="Image1" />
    </Image.GestureRecognizers>
</Image>

您可以在範例中找到此檢視模型的完整程式碼。 相關 Command 實作的詳細資料如下所示:

public class TapViewModel : INotifyPropertyChanged
{
    int taps = 0;
    ICommand tapCommand;
    public TapViewModel () {
        // configure the TapCommand with a method
        tapCommand = new Command (OnTapped);
    }
    public ICommand TapCommand {
        get { return tapCommand; }
    }
    void OnTapped (object s)  {
        taps++;
        Debug.WriteLine ("parameter: " + s);
    }
    //region INotifyPropertyChanged code omitted
}