Xamarin.Forms:添加点击事件进行映射 ?

问题描述 投票:0回答:1

当点击ListView时,我终于能够使用<Grid>显示button。这是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>

这就是代码隐藏:

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

单击按钮时,将显示ListView并隐藏按钮。到现在为止还挺好。

一旦ListView打开,当我点击地图时如何再次隐藏ListView?

我尝试使用GestureRecognizers<TapGestureRecognizer Tapped="OnTapGestureRecognizerTapped"/>,但它没有构建。

任何帮助表示赞赏。

我已经包含了截图,因为我还在学习术语。

enter image description here

c# xamarin xamarin.forms xamarin.android visual-studio-2017
1个回答
0
投票

在地图的父stacklayout上添加手势识别器

在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>

代码背后:

private void OnMapAreaTapped(object sen, EventArgs e)
        {
           listSection.IsVisible = false;
           Button_DisplayList.IsVisible = true;
        }
© www.soinside.com 2019 - 2024. All rights reserved.