xamarin - 如何在 Xamarin Forms 中显示一半屏幕大小的内容页面?
问题描述
我正在开发一个 Xamarin Forms 项目,需要实现以下目标:在从主页导航到新页面时,新页面将有一个菜单按钮、一些文本字段和一个签名按钮。当我们点击菜单按钮时,菜单页面应该向下滑动。幻灯片菜单页面应该有一个导航栏,并且应该能够导航到其他子菜单选项。
幻灯片菜单页面应与当前内容页面重叠。有什么办法可以实现吗?
解决方案
幻灯片菜单将仅由您要使用的包决定,或者如果您想使用 ResourceDictionary/VisualStateManager 创建动画,但要使其动态变为页面大小的一半,您可以使用以下内容:
XAML:
<Page x:Name="Page" HeightRequest="{Binding PageHeight, Mode=TwoWay}"></Page>
<Menu x:Name="Menu" HeightRequest="{Binding MenuHeight}"></Menu>
XAML.CS:
public class YourPage : YourType //(ContentViews for me)
private YourViewModel ViewModel => (YourViewModel)this.BindingContext;
public YourPage()
{
this.BindingContext = YourVM;
}
虚拟机.CS:
public class YourViewModel : INotifyPropertyChanged
private double _pageHeight;
public double PageHeight
{
set
{
if (_pageHeight != value)
{
_pageHeight = value;
OnPropertyChanged("PageHeight");
PageHeightChanged("PageHeight");
}
}
get
{
return _pageHeight;
}
}
private double _menuHeight;
public double MenuHeight
{
set
{
if (_menuHeight != value)
{
_menuHeight = value;
OnPropertyChanged("MenuHeight");
}
}
get
{
return _menuHeight;
}
}
private void PageHeightChanged()
{
Menu.HeightRequest = Page.Height/2;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}