我的应用程序非常简单:它在上半部分显示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>
这是模拟器中的应用程序:
当我点击地图时,我想隐藏ListView
,并在底部显示“Show List”。或多或少这样的事情:
我在类MainPage
(类似于trouble in Hide/show of listview on a click in xamarin android)中添加了一个类似的事件处理程序,但它没有构建:
public MainPage()
{
InitializeComponent();
/* Fills ListView and plots points in map */
ListView_Pets.ItemClick += OnListItemClick;
}
我建议使用<StackLayout>
来实现这种布局,而不是使用<Grid>
:
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 Framework,则可以使用绑定更新show hide逻辑。
以下步骤如下: