图像TapGestureRecognizer未触发

时间:2017-07-03 07:45:16

标签: xamarin.forms

我真的不明白为什么这个图像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。

感谢您的帮助。

由于

1 个答案:

答案 0 :(得分:3)

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

enter image description here

当我点击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>