簡體   English   中英

TapGestureRecognizer不以Xamarin形式響應

[英]TapGestureRecognizer not responding in xamarin forms

我首先嘗試捕獲屏幕上某些圖像的水龍頭時遇到一個很奇怪的問題,這是我的xaml代碼,用於實現為View的彈出窗口:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="NumbersRaceXamarin.YesNoPopupMessages">
  <ContentView.Content>
    <RelativeLayout HeightRequest="190" WidthRequest="280">
      <Image x:Name="bg" Source="compbar_01.png" Aspect="Fill" HeightRequest="190" WidthRequest="330" Opacity="0.9" InputTransparent ="true"/>
      <StackLayout WidthRequest="280" Spacing="0" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width , Factor=0.1,Constant=0}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height , Factor=0.02,Constant=0}">
        <Label x:Name="message" FontSize="15"></Label>
        <StackLayout WidthRequest="70" HeightRequest="30" Orientation="Horizontal" HorizontalOptions="Center"> 
          <RelativeLayout WidthRequest="30" HeightRequest="30" VerticalOptions="Center">
            <Image x:Name="minus" Source="startbottuns_01.png" Aspect="Fill" HeightRequest="30" WidthRequest="30"/>
            <Label  x:Name="minusTB" Text="-" FontSize="16" HeightRequest="30" WidthRequest="30" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"></Label>
          </RelativeLayout>
          <Label  x:Name="qtyTB" Text="1" FontSize="8" HeightRequest="30" WidthRequest="30" BackgroundColor="White" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"></Label>
          <RelativeLayout WidthRequest="30" HeightRequest="30" VerticalOptions="Center">
            <Image x:Name="plus" Source="startbottuns_01.png" Aspect="Fill" HeightRequest="30" WidthRequest="30" IsEnabled="true"/>

            <Label  x:Name="plusTB" Text="+" FontSize="16" HeightRequest="30" WidthRequest="30" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"></Label>
          </RelativeLayout>
        </StackLayout>
        <StackLayout x:Name="coinAmountSeg" HeightRequest="40" WidthRequest="90" Orientation="Horizontal"  HorizontalOptions="Center" IsVisible="true">
            <Label x:Name="coinsAmountTB"  Text="1200"  HeightRequest="35" WidthRequest="40" VerticalTextAlignment="Center" HorizontalTextAlignment="End"/>
            <Image x:Name="coinsPackIM" Source="coins_01.png"  HeightRequest="35" WidthRequest="25"/>

          </StackLayout>
           <StackLayout x:Name="yesNoBtSeg" HeightRequest="32" WidthRequest="150" Orientation="Horizontal"  HorizontalOptions="Center" IsVisible="true">
                <RelativeLayout  HorizontalOptions="Center" >   
                     <Image x:Name="yesBt" x:Uid="yesBt" Source="startbottuns_01.png" Aspect="Fill" HeightRequest="32" WidthRequest="80"></Image>
                     <Label  x:Name="yesTB" Text="OK" FontSize="20" HeightRequest="32" TextColor="Yellow" WidthRequest="80" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"></Label>
                </RelativeLayout >

                <RelativeLayout HorizontalOptions="Center" >   
                     <Image x:Name="noBt" x:Uid="noBt" Source="startbottuns_01.png" Aspect="Fill" HeightRequest="32" WidthRequest="80"></Image>
                     <Label  x:Name="noTB" Text="CANCEL" FontSize="20" HeightRequest="32" TextColor="Yellow" WidthRequest="80" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"></Label>
                </RelativeLayout>

          </StackLayout>
      </StackLayout>
    </RelativeLayout>
  </ContentView.Content>
</ContentView>

這是我嘗試從正負確定捕獲事件並取消圖像的方法:

    var minusTap = new TapGestureRecognizer();
    var plusTap = new TapGestureRecognizer();
    var okTap = new TapGestureRecognizer();
    var cancelTap = new TapGestureRecognizer();

    minusTap.Tapped += minusBtn_Click;
    plusTap.Tapped += plusBtn_Click;
    okTap.Tapped += okBtn_Click;
    cancelTap.Tapped += cancelBtn_Click;
    noBt.GestureRecognizers.Add(cancelTap);
    plus.GestureRecognizers.Add(plusTap);
    minus.GestureRecognizers.Add(minusTap);
    yesBt.GestureRecognizers.Add(okTap);
private void okBtn_Click(object sender, EventArgs e)
{
    tcs.SetResult(1);
}
private void cancelBtn_Click(object sender, EventArgs e)
{
    tcs.SetResult(0);
}
private void minusBtn_Click(object sender, EventArgs e)
{
    if (int.Parse(qtyTB.Text) > 1)
    {
        coinsAmountTB.Text = "" + (int.Parse(coinsAmountTB.Text) - itemCost);
        qtyTB.Text = (int.Parse(qtyTB.Text) - 1) + "";
    }
}
private void plusBtn_Click(object sender, EventArgs e)
{
    if (int.Parse(qtyTB.Text) < max)
    {
        coinsAmountTB.Text = "" + (int.Parse(coinsAmountTB.Text) + itemCost);
        qtyTB.Text = (int.Parse(qtyTB.Text) + 1) + "";
    }
}

這個問題真的很奇怪,點擊事件在“確定”取消和減號按鈕上正常運行,但是由於某種原因在點擊加號圖像時不會觸發。

我以為有些東西重疊了,並且“偷”了加號按鈕上的水龍頭,但是我找不到那樣的東西。 順便說一句,我正在android上嘗試,而不是在其他設備上嘗試。 任何人都不知道這種情況如何以及為什么發生?

好的,所以我發現了一個愚蠢的問題,認為如果有人遇到相同的問題,我會分享我的解決方案。 問題是包含按鈕的stacklayout的寬度小於它包含的元素的寬度,因此在這種情況下,最后一個元素加號超出了stacklayout的范圍,從而導致其無法獲得點擊手勢

<StackLayout WidthRequest="70" HeightRequest="30" Orientation="Horizontal" HorizontalOptions="Center"> 
         <RelativeLayout WidthRequest="30" HeightRequest="30" VerticalOptions="Center">

這行我只是將WidthRequest更改為100而不是70,因為它有3個元素,每個元素30寬度。

希望這可以幫助某人

我也遇到了同樣的問題,這也是由於父元素的大小小於啟用了Tap的子元素的大小所致。

就我而言,我有一個絕對布局( Inner內的另一個絕對布局() Outer )。 然后,我在后面的代碼中以編程方式將啟用了Tap的子元素添加到Inner中。

<AbsoluteLayout x:Name="Outer">
    <AbsoluteLayout x:Name="Inner" />
</AbsoluteLayout>

由於Inner沒有指定AbsoluteLayout.LayoutBounds ,因此交互周期似乎忽略了子級。 這對我來說並不明顯,因為子元素是可見的。

這工作:

<AbsoluteLayout x:Name="Outer">
    <AbsoluteLayout x:Name="Inner"
                    AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
                    AbsoluteLayout.LayoutFlags="All" />
</AbsoluteLayout>

但是,最終我將拍擊目標元素放在了Outer容器中,這也解決了這個問題,因為OuterContentPage包圍了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM