简体   繁体   English

Xamarin.Forms:添加点击事件以映射到 <Grid> ?

[英]Xamarin.Forms: Add click event to map in <Grid>?

I was finally able to display the ListView using a <Grid> when clicking on a button . 我终于能够在单击button时使用<Grid>显示ListView This is the xaml: 这是xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
             xmlns:local="clr-namespace:GasStations"
             x:Class="GasStations.MainPage">

    <Grid RowSpacing="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="50" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <StackLayout Grid.Row="0" x:Name="MapGrid">
            <maps:Map WidthRequest="960" HeightRequest="200" 
                  x:Name="MyMap" IsShowingUser="true"/>
        </StackLayout>
        <StackLayout Grid.Row="1">
            <Button Text="Show List" x:Name="Button_DisplayList"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="Center"
                Clicked="OnButtonClicked" />
        </StackLayout>
        <StackLayout Grid.Row="2" x:Name="listSection" IsVisible="false" HeightRequest="200">
            <ListView x:Name="ListView_Pets">
                <ListView.ItemsSource>
                    <x:Array Type="{x:Type x:String}">
                        <x:String>dog</x:String>
                        <x:String>cat</x:String>
                        <x:String>bird</x:String>
                    </x:Array>
                </ListView.ItemsSource>
            </ListView>
        </StackLayout>
    </Grid>
</ContentPage>

And this is the codebehind: 这是背后的代码:

void OnButtonClicked(object sender, EventArgs args)
{
    listSection.IsVisible = true;
    Button_DisplayList.IsVisible = false;
}

When I click the button, the ListView is displayed and the button is hidden. 当我单击按钮时,将显示ListView,并且按钮是隐藏的。 So far so good. 到现在为止还挺好。

Once the ListView is open, how can I hide the ListView again when I tap on the map? 打开ListView ,如何在点击地图时再次隐藏ListView?

I tried using GestureRecognizers and <TapGestureRecognizer Tapped="OnTapGestureRecognizerTapped"/> , but it doesn't build. 我尝试使用GestureRecognizers<TapGestureRecognizer Tapped="OnTapGestureRecognizerTapped"/> ,但它没有构建。

Any help is appreciated. 任何帮助表示赞赏。

I've included screenshots because I'm still learning the terminology. 我包括了屏幕截图,因为我仍在学习术语。

在此处输入图片说明

Adding gesture recognizer on the parent stacklayout of the map 在地图的父堆栈布局上添加手势识别器

On xaml: 在xaml上:

 <StackLayout Grid.Row="0" x:Name="MapGrid">
  <StackLayout.GestureRecognizer>
    <TapGestureRecognizer Tapped="OnMapAreaTapped"/>
  </StackLayout.GestureRecognizer>
       <maps:Map WidthRequest="960" HeightRequest="200" x:Name="MyMap" IsShowingUser="true"/>
  </StackLayout>

On code behind: 关于后面的代码:

private void OnMapAreaTapped(object sen, EventArgs e)
        {
           listSection.IsVisible = false;
           Button_DisplayList.IsVisible = true;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM