【问题标题】:Xamarin (Frame.)GestureRecognizers doesn't workXamarin (Frame.)GestureRecognizers 不起作用
【发布时间】:2018-11-13 10:09:41
【问题描述】:

我正在尝试在某些帧上制作 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>

使用此代码,我应该能够单击绿色部分并获得响应,但我没有得到响应?有没有办法让一些东西与整个框架重叠,这样我就可以用它来点击?

【问题讨论】:

  • 框架的内容当然会与它重叠,要么将其移动到 StackLayout,要么将其添加到 stacklayout 并保留两者(如果您有较大的边距/填充可以改善总触摸区域.. ..)
  • 喜欢这个? pastebin.com/04SU59HG。这也没有用。
  • 如果您拥有 Xamarin Inspector(企业许可证),您可以查看是否有东西重叠并阻止触摸输入 docs.microsoft.com/en-us/xamarin/tools/inspector 否则您可以为控件分配不同的背景颜色并这样做... .
  • 请查看编辑后的帖子。我没有 Xamarin 检查员。
  • 有人有想法吗?

标签: xaml xamarin


【解决方案1】:

原因:

您的Frame 没有回复您的TapGestureRecognizer 是由您的TranslationY="380" TranslationX="187.5" 属性引起的。即使看到此控件,您也无法点击超出父视图范围的控件。

翻译一个超出其父容器边界的元素可能 阻止输入工作。

解决方案:

删除TranslationYTranslationX 属性并以其他方式布置Frame

已编辑:

你可以看到下面的screenshort,我写了一个简单的demo并添加PaleGreen作为AbsoluteLayout的背景颜色。您的Frame 超出了父视图的范围。因此它不会响应TapGestureRecognizer

因此,将 FillAndExpand 设置为 AbsoluteLayout's VerticalOptions 属性以确保您的 Frame 在父视图的范围内。

<AbsoluteLayout BackgroundColor="PaleGreen" VerticalOptions="FillAndExpand">

我在这里更新了一个截图,希望你能理解:

【讨论】:

  • 感谢您的回复。我现在使用Margin 将框架放在正确的位置,但我不知道如何放置 GestureRecognizer 以使其与整个框架重叠。我该怎么办呢,因为目前我还没有回应。
  • @JipHarthoorn 查看我的编辑,如果您有任何问题,请回复我。
  • 谢谢。我现在使用此代码pastebin.com/JXfPsukB 我添加了FillAndExpand 和淡绿色,这就是i.gyazo.com/2c423f46d4b3ef1ecadc966020b95e42.jpg 的结果。但是,当我单击任何内容时,仍然没有任何反应。
  • 我只是复制你的代码并在Sport_Clicked 中添加一个断点。它确实进入了功能并且运行良好。还有其他代码吗?
  • 我再次测试,它确实有效。您能否创建一个新项目并添加您的代码Frame 以再次测试?并在代码后面的tap函数中添加breakpoint
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多