我想制作一个从按钮向下滚动的菜单 我的菜单是一个列表视图,列表视图在stacklayout内,所以我显示并显示使用更改容器堆栈布局的高度请求它工作正常,但我有一个问题,如果要开始隐藏菜单我的问题是堆栈布局总是-1,这意味着我认为不完全绑定的布局
<StackLayout
x:Name="ParentListViewStackLayout"
BackgroundColor="#242B3A"
Margin="0,0,0,0">
<Image x:Name="imgLogo" Margin="0,60,0,15"
VerticalOptions="Center"
/>
<Image Source="{local:ImageResource BlankApp.Images.menu1.png} "
VerticalOptions="CenterAndExpand">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="btnMenuClick"/>
</Image.GestureRecognizers>
</Image>
<StackLayout
Spacing="0"
x:Name="ListViewStackLayout">
<ListView x:Name="MenuListView" ItemsSource="{Binding
MenuModals}"
SeparatorColor="{StaticResource ControlTextColor}"
ItemSelected="MenuListView_OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<ViewCell.View>
<controls:CustomStackLayout>
<Label Text="{Binding MenuName}" FontSize="Medium" HorizontalOptions="Center" Style="{StaticResource LableStyle}" Margin="0,0,0,0"/>
<Label Text="{Binding MenuDescription}" FontSize="Micro" HorizontalOptions="Center" Style="{StaticResource LableStyle}" />
<Label Text="" FontSize="Micro" HorizontalOptions="Center" Style="{StaticResource LableStyle}" HeightRequest="25" />
</controls:CustomStackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</StackLayout>
代码Begind
public WashInternational()
{
InitializeComponent();
this.BindingContextChanged += OnBindingContextChanged;
MenuListView.BindingContextChanged += MenuListViewOnBindingContextChanged;
}
private void OnBindingContextChanged(object sender, EventArgs eventArgs)
{
ShowMenue(false);
}
private void MenuListViewOnBindingContextChanged(object sender, EventArgs eventArgs)
{
ShowMenue(false);
}
public void ShowMenue(bool isShow)
{
if (isShow == _isMenuShow) return;
var height = ListViewStackLayout.Height;
if (_menuHeight <= 0)
{
_menuHeight = height;
}
_isMenuShow = isShow;
if (isShow)
{
//MenuListView.TranslateTo(0, 0, 1000);
ParentListViewStackLayout.HeightRequest = ParentListViewStackLayout.Height + _menuHeight;
//ListViewStackLayout.ScaleTo(1, 500, Easing.Linear);
}
else
{
ParentListViewStackLayout.HeightRequest = ParentListViewStackLayout.Height - _menuHeight;
//MenuListView.TranslateTo(0,- 500, 1000);
//ListViewStackLayout.ScaleTo(0, 500, Easing.Linear);
}
}
代码工作正常,但我在初始状态下的问题我无法启动我的应用程序而且菜单是隐藏的,因为我不知道正确的位置来访问我的堆栈布局的高度
答案 0 :(得分:0)
我没有找到一个可以在开头获取高度的方法。我建议如果您不想通过菜单启动应用程序,则不应该在开头设置BindingContext,而是在要显示菜单时设置它,例如,按钮单击事件
private void Button_Clicked(object sender, EventArgs e)
{
List<Item> list = new List<Item>();
list.Add(new Item { MenuName = "1", MenuDescription = "111" });
list.Add(new Item { MenuName = "2", MenuDescription = "222" });
list.Add(new Item { MenuName = "3", MenuDescription = "333" });
BindingContext = new Model { List = list};
}
因此将显示listView,并且您可以在那时轻松获得其包含stackLayout的高度。