我想从屏幕中的按钮向下滚动菜单



我想制作一个从按钮向下滚动的菜单 我的菜单是一个列表视图,列表视图位于堆栈布局内,所以我使用 更改容器堆栈布局的高度请求 它工作正常,但如果要从隐藏菜单开始,我有一个问题 我的问题是堆栈布局总是-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>

代码开始

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

}

代码工作正常,但我的问题在初始状态下我无法启动我的应用程序并且菜单被隐藏,因为我不知道访问堆栈布局高度的正确位置

我没有找到可以在开始时获取高度的方法。我建议如果你不想用菜单启动你的应用程序,你不应该在开始时设置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};
}

所以列表视图将显示,并且您可以在那时轻松获得其包含堆栈布局的高度。

最新更新