Xamarin(框架)GestureRecognizers不起作用

时间:2018-11-09 21:59:41

标签: xaml xamarin

我正在尝试在某些框架上制作TapGestureRecognizer,但是当我对其进行测试时,什么也没发生。

xaml

<StackLayout>
<AbsoluteLayout>
    <Frame HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" TranslationY="380" TranslationX="12.5" HeightRequest="60" WidthRequest="120">
        <Frame.GestureRecognizers>
            <TapGestureRecognizer Tapped="Sport_Clicked"/>
        </Frame.GestureRecognizers>
        <StackLayout Orientation="Vertical" Padding="0" HorizontalOptions="FillAndExpand">
            <StackLayout Orientation="Horizontal" Spacing="-10">
                <Label Text="Sport" FontSize="17"/>
            </StackLayout>
        </StackLayout>
    </Frame>

    <Frame HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" TranslationY="380" TranslationX="187.5" HeightRequest="60" WidthRequest="120">
        <Frame.GestureRecognizers>
            <TapGestureRecognizer Tapped="Voeding_Clicked"/>
        </Frame.GestureRecognizers>

        <StackLayout Orientation="Vertical" Padding="0" HorizontalOptions="FillAndExpand">
            <StackLayout Orientation="Horizontal" Spacing="-10">
                <Label Text="Voeding" FontSize="17"/>
            </StackLayout>
        </StackLayout>
    </Frame>

    <Frame HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" TranslationY="495" TranslationX="12.5" HeightRequest="60" WidthRequest="120">
        <Frame.GestureRecognizers>
            <TapGestureRecognizer Tapped="Slaap_Clicked"/>
        </Frame.GestureRecognizers>
        <StackLayout Orientation="Vertical" Padding="0" HorizontalOptions="FillAndExpand">
            <StackLayout Orientation="Horizontal" Spacing="-10">
                <Label Text="Slaap" FontSize="17" />
            </StackLayout>
        </StackLayout>
    </Frame>
</AbsoluteLayout>
</StackLayout>

cs

void Sport_Clicked(object sender, EventArgs e)
{
    new NavigationPage(new SportPage());
}

void Voeding_Clicked(object sender, EventArgs e)
{
    new NavigationPage(new VoedingPage());
}

void Slaap_Clicked(object sender, EventArgs e)
{
    new NavigationPage(new SlaapPage());
}

测试时我没有收到任何警告或错误。是否有与您看不见的帧重叠的东西导致输入阻塞?

编辑:

这是带有以下代码的框架的外观

<Frame HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" TranslationY="380" TranslationX="187.5" HeightRequest="60" WidthRequest="120" BackgroundColor="Yellow">
    <StackLayout Orientation="Vertical" Padding="0" HorizontalOptions="FillAndExpand" BackgroundColor="Green">
        <StackLayout.GestureRecognizers>
            <TapGestureRecognizer Tapped="Voeding_Clicked"/>
        </StackLayout.GestureRecognizers>
        <StackLayout Orientation="Horizontal" Spacing="-10" BackgroundColor="Red">
            <Label Text="Voeding" FontSize="17"/>
        </StackLayout>
    </StackLayout>
</Frame>

image

使用此代码,我应该能够单击绿色部分并获得响应,但是我没有得到?有什么办法可以使整个框架重叠,以便我可以点击它?

1 个答案:

答案 0 :(得分:1)

原因:

您的bulk_create对您的Frame没有反应是由您的TapGestureRecognizer属性引起的。即使看到了该控件,也无法点击父视图范围之外的控件。

  

在其父容器的边界之外翻译元素可能会   阻止输入正常工作。

解决方案:

删除TranslationY="380" TranslationX="187.5"TranslationY属性,并以其他方式布置TranslationX

已编辑:

您可以在下面看到屏幕截图,我编写了一个简单的演示并将Frame添加为PaleGreen的backgroundColor。您的AbsoluteLayout超出了父视图的范围,因此不会响应Frame

因此,将TapGestureRecognizer设置为FillAndExpand AbsoluteLayout's属性,以确保您的Frame位于父视图的边界之内。

VerticalOptions

我在这里更新了屏幕截图,希望您能很好理解:

subViewOutOfParentView