Xamarin.Forms:添加点击事件以在<grid>中进行映射吗?

时间:2019-02-14 02:05:31

标签: c# xamarin xamarin.forms xamarin.android visual-studio-2017

我最终能够在点击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

1 个答案:

答案 0 :(得分:0)

在地图的父堆栈布局上添加手势识别器

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