在特定平台上添加TapGestureRecognizer

时间:2018-05-30 20:59:06

标签: xamarin.forms

是否可以仅在UWP上添加TapGestureRecognizer?像这样的东西

<OnPlatform x:TypeArguments="GestureRecognizers">
    <OnPlatform.Platforms>
        <On Platform="UWP">
            <TapGestureRecognizer Command="{Binding MyUWPCommand}"/>
        </On>
    </OnPlatform.Platforms>
</OnPlatform>

3 个答案:

答案 0 :(得分:1)

为什么不在MyUWPCommand的代码中检查当前平台是否为UWP?如果是这样,执行代码,如果没有,则不执行任何操作。

答案 1 :(得分:1)

在XAML中:

<OnPlatform x:TypeArguments="Label">
    <On Platform="UWP">
        <Label Text="TapGestureRecognizer for UWP">
            <Label.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding MyUWPCommand}" />
            </Label.GestureRecognizers>
        </Label>
    </On>
    <On Platform="Android, iOS">
        <Label Text="No GestureRecognizers for Android, iOS" />
    </On>
</OnPlatform>

答案 2 :(得分:0)

只需要将每个 GestureRecognizer 包装在 OnPlatform 中

<ContentView.GestureRecognizers>
  <OnPlatform x:TypeArguments="GestureRecognizer">
    <OnPlatform.Platforms>
        <On Platform="UWP">
            <TapGestureRecognizer Command="{Binding MyUWPCommand}"/>
        </On>
    </OnPlatform.Platforms>
  </OnPlatform>
</ContentView.GestureRecognizers>