Android 表单:在地图上隐藏列表视图单击?

Android 表单:在地图上隐藏列表视图单击?,android,xamarin,xamarin.forms,visual-studio-2017,xamarin.forms.listview,Android,Xamarin,Xamarin.forms,Visual Studio 2017,Xamarin.forms.listview,我的应用程序非常简单:它在上半部分显示一个Xamarin.Forms.Map,在下半部分显示一个ListView 这是我的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"

我的应用程序非常简单:它在上半部分显示一个
Xamarin.Forms.Map
,在下半部分显示一个
ListView

这是我的
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">

    <StackLayout>
        <StackLayout VerticalOptions="FillAndExpand">
            <maps:Map WidthRequest="960" HeightRequest="200"
            x:Name="MyMap"
            IsShowingUser="true"/>
            <ListView x:Name="ListView_Pets">
            </ListView>
        </StackLayout>
    </StackLayout>

</ContentPage>

以下步骤如下:

  • 添加按钮或标签,以便在视图中的stacklayout内显示“显示列表”
  • 现在为map、listview和button和创建命令和属性
    通过绑定从视图模型处理
  • 按地图上的键时,在视图模型中调用自定义命令并写入 隐藏列表可见性、根据高度调整大小的逻辑 以及显示按钮可见性
  • 按on按钮时,在中调用click custom命令 查看模型并编写显示列表可见性的逻辑, 调整高度和隐藏按钮可见性的大小

    • 以下步骤如下:

      • 添加按钮或标签,以便在视图中的stacklayout内显示“显示列表”
      • 现在为map、listview和button和创建命令和属性
        通过绑定从视图模型处理
      • 按地图上的键时,在视图模型中调用自定义命令并写入 隐藏列表可见性、根据高度调整大小的逻辑 以及显示按钮可见性
      • 按on按钮时,在中调用click custom命令 查看模型并编写显示列表可见性的逻辑, 调整高度和隐藏按钮可见性的大小
      我建议使用
      来实现这种布局,而不是使用

      Xaml代码:

      <Grid RowSpacing="0">
          <Grid.RowDefinitions>
              <RowDefinition Height="*" />
              <RowDefinition Height="50" />
              <RowDefinition Height="auto" />
          </Grid.RowDefinitions>
          <StackLayout Grid.Row="0">
              <maps:Map WidthRequest="960" HeightRequest="200" 
                        x:Name="MyMap" IsShowingUser="true"/>
          </StackLayout>
          <StackLayout Grid.Row="1">
              <Label Text="Show List" TextColor="LightGray">
                  <Label.GestureRecognizers>
                      <TapGestureRecognizer Tapped="OnTapGestureRecognizerTapped"/>
                  </Label.GestureRecognizers>
              </Label>
          </StackLayout>
          <StackLayout Grid.Row="2" x:Name="listSection" IsVisible="false" HeightRequest="200">
              <ListView x:Name="ListView_Pets"/>
          </StackLayout>
      </Grid>
      
      private bool isListVisible;
      void OnTapGestureRecognizerTapped(object sender, EventArgs args)
       {
          isListVisible = !isListVisible;
          listSection.IsVisible = !isListVisible;
       }
      
      如果您使用的是MVVM框架,则可以使用绑定更新显示隐藏逻辑。

      而不是使用
      我建议使用
      来实现这种布局:

      Xaml代码:

      <Grid RowSpacing="0">
          <Grid.RowDefinitions>
              <RowDefinition Height="*" />
              <RowDefinition Height="50" />
              <RowDefinition Height="auto" />
          </Grid.RowDefinitions>
          <StackLayout Grid.Row="0">
              <maps:Map WidthRequest="960" HeightRequest="200" 
                        x:Name="MyMap" IsShowingUser="true"/>
          </StackLayout>
          <StackLayout Grid.Row="1">
              <Label Text="Show List" TextColor="LightGray">
                  <Label.GestureRecognizers>
                      <TapGestureRecognizer Tapped="OnTapGestureRecognizerTapped"/>
                  </Label.GestureRecognizers>
              </Label>
          </StackLayout>
          <StackLayout Grid.Row="2" x:Name="listSection" IsVisible="false" HeightRequest="200">
              <ListView x:Name="ListView_Pets"/>
          </StackLayout>
      </Grid>
      
      private bool isListVisible;
      void OnTapGestureRecognizerTapped(object sender, EventArgs args)
       {
          isListVisible = !isListVisible;
          listSection.IsVisible = !isListVisible;
       }
      
      如果您使用的是MVVM框架,则可以使用绑定更新显示隐藏逻辑