首页 \ 问答 \ 图像TapGestureRecognizer未触发(Image TapGestureRecognizer not firing)

图像TapGestureRecognizer未触发(Image TapGestureRecognizer not firing)

我真的不明白为什么这个图像TapGestureRecognizer没有开火。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MainMenu">   

    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness"
                iOS="0, 20, 0, 0"
                Android="0, 0, 0, 0"
                WinPhone="0, 0, 0, 0" />
    </ContentPage.Padding>

    <ContentPage.Content>

        <Grid>

            <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" RowSpacing="0">

                <Grid.RowDefinitions>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <BoxView BackgroundColor="AliceBlue" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="5"/>
                <Image Source="itemIcon1" Grid.Row="0" Grid.Column="0" Aspect="AspectFit" BackgroundColor="Transparent" WidthRequest="30" HeightRequest="30" />
                <Label Text = "Cpyname" FontSize="16" Grid.Row="0" Grid.Column="1"  Grid.ColumnSpan="2"/>


                <Grid Grid.Row="1">

                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width ="200"/>
                            <ColumnDefinition Width ="10"/>
                            <ColumnDefinition Width ="200"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>


                        <Grid Grid.Row="0" Grid.ColumnSpan="3">
                            <StackLayout  HeightRequest="80" Orientation="Horizontal">
                            </StackLayout>
                        </Grid>



                        <StackLayout Grid.Row="1" Grid.Column="0" WidthRequest="110" Orientation="Vertical">
                            <Image x:Name="ImgRepairSvc" Source="M1.png" HeightRequest="100" WidthRequest="100"/>
                        </StackLayout>

                        <StackLayout Grid.Row="1" Grid.Column="2" WidthRequest="110" Orientation="Vertical">
                            <Image Source="M2.png" HeightRequest="100" WidthRequest="100"/>
                        </StackLayout>


                    </Grid>
                </Grid>

            </Grid>
        </Grid>

    </ContentPage.Content>
</ContentPage>

这里的代码:

1) XAML :

 <StackLayout Grid.Row="1" Grid.Column="0" WidthRequest="110" Orientation="Vertical">
   <Image x:Name="ImgSvc" Source="M1.png" HeightRequest="100" WidthRequest="100"/>  
</StackLayout>


2) Code behind the XAML:

public MainMenu 
{           
     InitializeComponent ();            
     SetUp();

}


 void SetUp()
 {

    ImgSvc.GestureRecognizers.Add(new TapGestureRecognizer()
    {
       Command = new Command(TapCallback)

    });

  }


  private void TapCallback()
  {
     DisplayAlert("Tap", "This is image Tap", "Ok");

  }

在Emulator(Droid)上编译成功,但是当使用鼠标单击图像时,没有displayAlert。

感谢您的帮助。

谢谢


I dont really understand why this image TapGestureRecognizer not firing.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MainMenu">   

    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness"
                iOS="0, 20, 0, 0"
                Android="0, 0, 0, 0"
                WinPhone="0, 0, 0, 0" />
    </ContentPage.Padding>

    <ContentPage.Content>

        <Grid>

            <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" RowSpacing="0">

                <Grid.RowDefinitions>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <BoxView BackgroundColor="AliceBlue" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="5"/>
                <Image Source="itemIcon1" Grid.Row="0" Grid.Column="0" Aspect="AspectFit" BackgroundColor="Transparent" WidthRequest="30" HeightRequest="30" />
                <Label Text = "Cpyname" FontSize="16" Grid.Row="0" Grid.Column="1"  Grid.ColumnSpan="2"/>


                <Grid Grid.Row="1">

                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width ="200"/>
                            <ColumnDefinition Width ="10"/>
                            <ColumnDefinition Width ="200"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>


                        <Grid Grid.Row="0" Grid.ColumnSpan="3">
                            <StackLayout  HeightRequest="80" Orientation="Horizontal">
                            </StackLayout>
                        </Grid>



                        <StackLayout Grid.Row="1" Grid.Column="0" WidthRequest="110" Orientation="Vertical">
                            <Image x:Name="ImgRepairSvc" Source="M1.png" HeightRequest="100" WidthRequest="100"/>
                        </StackLayout>

                        <StackLayout Grid.Row="1" Grid.Column="2" WidthRequest="110" Orientation="Vertical">
                            <Image Source="M2.png" HeightRequest="100" WidthRequest="100"/>
                        </StackLayout>


                    </Grid>
                </Grid>

            </Grid>
        </Grid>

    </ContentPage.Content>
</ContentPage>

Here the code:

1) XAML :

 <StackLayout Grid.Row="1" Grid.Column="0" WidthRequest="110" Orientation="Vertical">
   <Image x:Name="ImgSvc" Source="M1.png" HeightRequest="100" WidthRequest="100"/>  
</StackLayout>


2) Code behind the XAML:

public MainMenu 
{           
     InitializeComponent ();            
     SetUp();

}


 void SetUp()
 {

    ImgSvc.GestureRecognizers.Add(new TapGestureRecognizer()
    {
       Command = new Command(TapCallback)

    });

  }


  private void TapCallback()
  {
     DisplayAlert("Tap", "This is image Tap", "Ok");

  }

Compiling is successful on Emulator (Droid), but when use mouse to click the image, there is no displayAlert.

Your help is appreciated.

Thanks


原文:https://stackoverflow.com/questions/44880227
更新时间:2019-12-07 10:26

最满意答案

我认为大多数为什么这不能正常工作是你正在使用的所有GridStackLayout控件的混乱结构。 您有一个网格,其列Width设置为40.在该列中,您添加的Grid具有Width为200的列和Width为110的Image 。 你可以在这张图片中看到它:

在此处输入图像描述

当我单击Image (红色)时, TapGestureRecognizer仅在您单击紫红色的Grid元素内的图像的左侧部分时触发。 外部的所有内容都在Image的父级外部呈现,不会触发TapGestureRecognizer

我的建议是清理你的XAML,因为我无法想象你正在使用所有嵌套的StackLayoutGrid控件创建一个需要这种复杂性的布局。

这里是为每个元素设置BackgroundColor的代码,以防您想要自己查看。

 <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness"
                iOS="0, 20, 0, 0"
                Android="0, 0, 0, 0"
                WinPhone="0, 0, 0, 0" />
    </ContentPage.Padding>
    <ContentPage.Content>
        <Grid BackgroundColor="Yellow">
            <Grid BackgroundColor="Blue" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" RowSpacing="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <BoxView BackgroundColor="AliceBlue" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="5"/>
                <Image BackgroundColor="Olive" Source="itemIcon1" Grid.Row="0" Grid.Column="0" Aspect="AspectFit" WidthRequest="30" HeightRequest="30" />
                <Label BackgroundColor="Gray" Text = "Cpyname" FontSize="16" Grid.Row="0" Grid.Column="1"  Grid.ColumnSpan="2"/>

                <Grid Grid.Row="1" BackgroundColor="Teal">
                    <Grid BackgroundColor="Fuchsia">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200"/>
                            <ColumnDefinition Width="10"/>
                            <ColumnDefinition Width="200"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <Grid BackgroundColor="Lime" Grid.Row="0" Grid.ColumnSpan="3">
                            <StackLayout HeightRequest="80" Orientation="Horizontal">
                            </StackLayout>
                        </Grid>

                        <StackLayout BackgroundColor="Maroon" Grid.Row="1" Grid.Column="0" WidthRequest="110" Orientation="Vertical">
                            <Image x:Name="ImgRepairSvc" BackgroundColor="Red" Source="M1.png" HeightRequest="100" WidthRequest="100"/>
                        </StackLayout>

                        <StackLayout BackgroundColor="Purple" Grid.Row="1" Grid.Column="2" WidthRequest="110" Orientation="Vertical">
                            <Image Source="M2.png" HeightRequest="100" WidthRequest="100"/>
                        </StackLayout>
                    </Grid>
                </Grid>
            </Grid>
        </Grid>
    </ContentPage.Content>

I think most of why this is not working correctly is the chaotic structure of all the Grid and StackLayout controls you're using. You have a grid with a column that has a Width set to 40. In that column you're adding a Grid that has a column which has a Width of 200 and an Image that is 110 wide. You can see it in this image:

enter image description here

When I click on the Image (red) the TapGestureRecognizer only triggers when you click the left part of the image that is within the Fuchsia colored Grid element. Everything outside that is being rendered outside the parent of the Image won't trigger the TapGestureRecognizer.

My advice is to clean up your XAML because I can't imagine you're creating a layout that needs this kind of complexity with all the nested StackLayout and Grid controls.

Here's the code with a BackgroundColor set to each element in case you want to see for yourself.

 <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness"
                iOS="0, 20, 0, 0"
                Android="0, 0, 0, 0"
                WinPhone="0, 0, 0, 0" />
    </ContentPage.Padding>
    <ContentPage.Content>
        <Grid BackgroundColor="Yellow">
            <Grid BackgroundColor="Blue" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" RowSpacing="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <BoxView BackgroundColor="AliceBlue" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="5"/>
                <Image BackgroundColor="Olive" Source="itemIcon1" Grid.Row="0" Grid.Column="0" Aspect="AspectFit" WidthRequest="30" HeightRequest="30" />
                <Label BackgroundColor="Gray" Text = "Cpyname" FontSize="16" Grid.Row="0" Grid.Column="1"  Grid.ColumnSpan="2"/>

                <Grid Grid.Row="1" BackgroundColor="Teal">
                    <Grid BackgroundColor="Fuchsia">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200"/>
                            <ColumnDefinition Width="10"/>
                            <ColumnDefinition Width="200"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <Grid BackgroundColor="Lime" Grid.Row="0" Grid.ColumnSpan="3">
                            <StackLayout HeightRequest="80" Orientation="Horizontal">
                            </StackLayout>
                        </Grid>

                        <StackLayout BackgroundColor="Maroon" Grid.Row="1" Grid.Column="0" WidthRequest="110" Orientation="Vertical">
                            <Image x:Name="ImgRepairSvc" BackgroundColor="Red" Source="M1.png" HeightRequest="100" WidthRequest="100"/>
                        </StackLayout>

                        <StackLayout BackgroundColor="Purple" Grid.Row="1" Grid.Column="2" WidthRequest="110" Orientation="Vertical">
                            <Image Source="M2.png" HeightRequest="100" WidthRequest="100"/>
                        </StackLayout>
                    </Grid>
                </Grid>
            </Grid>
        </Grid>
    </ContentPage.Content>
2017-07-03

相关问答

更多

NSImage和UIImage提供不同的NSData表示(NSImage and UIImage give different NSData representations)

我最终找到了一种方法来做我想做的事。 ALAssetRepresentation类的getBytes:fromOffset:length:error:方法可用于获取与OS X中的[NSData dataWithContentsOfURL:fileURL]相同的NSData对象。请注意,由于UIImage执行某些处理,因此无法从UIImage执行此操作。在图像上。 这是所请求的功能的样子。 //In iOS -(NSData *) getDataFromALAsset:(ALAsset*)asse

在javascript对象方法中使用eventListener定位节点(locating node with eventListener in javascript object method)

当元素具有eventhandler时,Event对象将传递给事件处理程序。 此事件对象除其他外还引用了触发事件的节点。 所以... var createSibling = function(_event){ var e = this; // the element this function is actually attached to var event_e = _event.target; //another way to get at the element clicked

将格式化程序附加到iPhone上的UITextField(Attach a formatter to a UITextField on iPhone)

我相信UITextView有一个回调,每次内容发生变化时都会触发,然后你可以通过格式化程序运行整个内容。 编辑:对,找到Docs Here UITextViewTextDidChangeNotification 应该很容易实现,但是,抬头,当用户输入合理数量的文本时,每次按键都会调用你的代码,我建议在之前检查的文本和新文本之间做一些差异,否则你每次按键都会检查一大块文本(例如,您可以添加时间延迟?) I believe UITextView has a callback that fires e

从模板调用方法(Calling a method from template)

在我看来,你有点复杂。 class Gallery(models.Model): class Meta: verbose_name_plural = "Galerie" def random_image(self): return self.photo_set.order_by('?')[0] if self.photo_set.count() else None title = models.CharField(max_length=

如何在此事件处理程序中避免重复代码?(How do I avoid repetitive code in this event handler?)

一个简单的“意见”:使用Map<SomeButtonType, StoreType> ,其中SomeButtonType是源的类型,StoreType是任何类型存储,它会将您的交换机处理程序减少到一到两行: private void handleButtonAction(ActionEvent event) { Object source = event.getSource(); store = myMap.get(source); } 当然,您需要在创建按钮时填充地图,但这应

cpu cacheline和prefetch策略(cpu cacheline and prefetch policy)

简答(TL; DR):您正在访问未初始化的数据,您的第一个循环必须在定时循环内为整个阵列分配新的物理页面。 当我运行你的代码并依次评论每个部分时,我得到两个循环几乎相同的时间。 但是,当我取消注释两个部分并一个接一个地运行它们时,我确实得到了相同的结果。 这让我怀疑你也是这样做的,并且在比较第一个循环和第二个循环时遭受冷启动效应。 它很容易检查 - 只需替换循环的顺序,看看第一个循环是否仍然较慢。 要避免,要么选择一个足够大的LENGTH (取决于你的系统),这样你就不会从第一个循环中获得任何缓

使用ASP.NET MVC的jQuery TreeView如何将参数传递给url(jQuery TreeView with ASP.NET MVC how to pass argument to url)

由于yoiu可以看到你是否在演示中使用Firebug,扩展节点会触发数据的GET请求: http://view.jquery.com/trunk/plugins/treeview/demo/source.php?root=36 响应只是JSON: [ { "text": "1. Review of existing structures", "expanded": true, "children": [

Padrino 0.12.0,Compass 0.12.2,haml 4.0.5和Zurb Foundation 5(“camorra”插件) - javascripts不工作(Padrino 0.12.0, Compass 0.12.2, haml 4.0.5 and Zurb Foundation 5 (“camorra” plugin) - javascripts not working)

检查页面时我注意到的一些事情: 加载Modernizr时出现404错误:您的页面正在尝试从/javascripts/modernizr.js加载Modernizr,但该文件实际上位于/javascripts/vendor/modernizr.js (在vendor文件夹中)。 缺少基础JavaScript插件:您的页面正在加载/javascripts/foundation/foundation.js但它只包含Foundation核心而没有任何插件(顶部栏下拉菜单和Orbit滑块实现为插件)。 您

相关文章

更多

最新问答

更多
  • jsPlumb draggable element javascript函数(jsPlumb draggable element javascript function)
  • MVC4:ViewModel(带有radiobuttonlist)在HttpPost之后为空(MVC4: ViewModel (with radiobuttonlist) is empty after HttpPost)
  • 如何在同一帐户上设置“Dev repo”(在prod和团队之间)(How to set up a “Dev repo” (between the prod and the team) on the same account)
  • 如何在tcl中将eth0配置为发送方udp端口(how to configure eth0 as a sender udp port in tcl)
  • 在main方法中进行更改后传递给构造函数的TreeMap实例的行为是什么(What is the behavior of a TreeMap instance passed into a constructor following changes in main method)
  • 在这个的一些属性不同的颜色(Different color in some properties of this)
  • CURL没有返回任何内容(CURL Not returning anything)
  • MVC5注入依赖于城堡windsor的视图(MVC5 injecting dependency on a view with castle windsor)
  • CakePHP AJAX-Call:发生错误:未定义(CakePHP AJAX-Call: An error occured: undefined)
  • 如何用.aidl扩展名创建文件?(how to create a file with .aidl extension?)
  • 无法获得全尺寸动态WordPress灯箱图像显示(Cannot get full size dynamic WordPress lightbox image to display)
  • 使用Jsoup删除元素不起作用(Removing Element with Jsoup doesn't work)
  • 交叉编译ARM的MongoDB C ++驱动程序。(Cross compiling MongoDB C++ driver for ARM. Cannot find Boost Libraries)
  • ProgressDialog没有关闭(ProgressDialog not closing)
  • 单元测试DB中的JPA插入(Unit testing JPA insertion in the DB)
  • 点击谷歌服务对话框中的更新按钮(Click on update button in check google service dialog)
  • 适用于PhoneGap Build的iOS“缺少必需的57x57图标”(“Missing required 57x57 icon” for iOS on PhoneGap Build)
  • MVC ckeditor post编辑器值内联(MVC ckeditor post editor value inline)
  • Angular 2 Pipe - 无法读取未定义的属性'toString'(Angular 2 Pipe - Cannot read property 'toString' of undefined)
  • 安装引导加载程序(Installing bootloaders)
  • 用于确定最终如何包含头文件的工具(Tool to figure out how a header file is eventually being included)
  • 在将字符串转换为int [duplicate]之前检查字符串是否不是数字(Check if string is not a number before converting it to a int [duplicate])
  • 从脚本构建db2数据库以进行构建验证的工具?(Tools to build db2 database from scripts for build verification?)
  • 在偏移之后向FileOutputStream写一个数字(Write a number to FileOutputStream after an offset)
  • Javascript RegEx仅限数字(无特殊字符)(Javascript RegEx for Numbers Only (no special characters))
  • 我可以在PHP的Linux服务器上运行新的COM(“WScript.Shell”)(Can I run new COM(“WScript.Shell”) on linux servers in PHP)
  • 在Backbone.js中放置与视图不直接相关的代码(例如广告,分析等)的位置?(Where to place code not directly related to a view (such as ads, analytics etc) in Backbone.js?)
  • 适用于Mac和PC的一般开发人员必备软件综合列表[关闭](Comprehensive List of Essential Software for General Developers on Mac and PC [closed])
  • 将语言文本转换为模型(Transformation Language Text to Model)
  • 在控制器中的两个方法之间共享变量?(Sharing variables between two methods in a controller?)