如何在Xamarin.Forms中创建底部表?
https://material.io/guidelines/components/bottom-sheets.html
https://material.io/guidelines/components/bottom-sheets.html
使用 Xamarin Forms 创建底部菜单而无需使用 NuGet 包库。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourAppName.Views.YourPageName">
<ContentPage.Content>
<RelativeLayout BackgroundColor="White">
<StackLayout HorizontalOptions="Fill" VerticalOptions="Fill">
<!-- place your page content here -->
</StackLayout>
<Frame x:Name="BottomSheet" CornerRadius="20" HasShadow="True" BackgroundColor="White" Padding="10,5" BorderColor="LightGray"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=.93,Constant=0}"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1,Constant=0}"
RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=1,Constant=0}">
<Frame.GestureRecognizers>
<PanGestureRecognizer PanUpdated="OnPanelUpdated" />
</Frame.GestureRecognizers>
<StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="Fill">
<BoxView HeightRequest="5" CornerRadius="2" BackgroundColor="Grey" HorizontalOptions="CenterAndExpand" WidthRequest="50" Margin="25,10,0,10"></BoxView>
<!-- Place your bottom sheet layout here -->
</StackLayout>
</Frame>
</RelativeLayout>
</ContentPage.Content>
</ContentPage>
YourPageName.xaml.cs的代码后台
protected override void OnAppearing()
{
MoveBottomSheet(true);
}
/// The -negative value determines how many vertical units should the panel occuply on the screen.
private async void MoveBottomSheet(bool close)
{
double finalTranslation = close ? (Device.Idiom == TargetIdiom.Phone ? -134.0 : -144.0) : (Device.Idiom == TargetIdiom.Phone ? -389.0 : -434.0);
await BottomSheet.TranslateTo(BottomSheet.X, finalTranslation, 450, Easing.SinIn);
}
/// This is fired multiple times while the user pans the bottom sheet. This variable captures the first intention of determining whether to open (pan up) or close (pan down)
bool _panelActivated = false;
private void OnPanelUpdated(object sender, PanUpdatedEventArgs e)
{
switch (e.StatusType)
{
case GestureStatus.Started:
break;
case GestureStatus.Running:
if (_panelActivated)
{
return;
}
MoveBottomSheet(e.TotalY > 0);
_panelActivated = true;
break;
case GestureStatus.Completed:
_panelActivated = false;
break;
case GestureStatus.Canceled:
break;
}
}
你可以使用我创建的NuGet包,这是该GitHub项目
https://github.com/josco007/CHDBottomSheet
它还支持内部的scrollviews。protected override void OnAppearing()
{
base.OnAppearing();
CHDBottomSheetBs.SetRelativeLayout(_rootRlt);
}