首页 > 解决方案 > 在 Xamarin 表单中向下滚动时关闭 Rg.Plugins.Popup

问题描述

我在我的 xamarin 表单应用程序中使用Rg.Plugins.Popup插件。这是一个很好的模态对话框插件。但是,我希望在用户向下滚动时关闭对话框。(iOS 中的大多数对话框都有这种向下滚动关闭的行为)。

弹出页面内的 XAML。

选项1

 <pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
             HasSystemPadding="True"
             CloseWhenBackgroundIsClicked="False"
             x:Name="MyPage"
             x:Class="wQuran.Views.Today.PrayerTimesSettingsPopupPage">
<Frame Style="{DynamicResource PopupFrame}" VerticalOptions="FillAndExpand" IsClippedToBounds="True">
        <Grid RowDefinitions="Auto,*" Style="{DynamicResource BaseGrid}">
            <BoxView Grid.Row="0" Style="{DynamicResource PopupTitleBoxView}"/>
            <Grid Grid.Row="0" ColumnDefinitions="*,30" HeightRequest="40" Padding="10">
                <Label Grid.Column="0" Text="{Binding Title}" Style="{DynamicResource PopupTitleLabel}"/>
                <ImageButton Grid.Column="1" Style="{DynamicResource DialogCloseImageButton}" Command="{Binding CloseDialogCommand}"/>
            </Grid>
        </Grid>
    </Frame>
</pages:PopupPage>

选项 2. 更新 XAML 并在 ScrollView 中添加 Frame

<ScrollView Scrolled="ScrollView_Scrolled">
....
</ScrollView>

private void ScrollView_Scrolled(object sender, Xamarin.Forms.ScrolledEventArgs e)
        {
            if (e.ScrollY > 100)
            {
                itemViewModel.CloseDialogCommand.Execute(null);   
            }
        }

在选项 1 中,我没有滚动,因此插件以默认行为工作,但我无法关闭滚动中的对话框。

在选项 2 中,我在滚动视图中添加了框架以检查 Y 滚动并关闭对话框。Scrolled 事件永远不会触发。此外,在模式外部单击时,我无法关闭对话框。

毕竟,我的问题是如何在向下滚动时关闭对话框?

标签: xamarin.formsxamarin.androidxamarin.ios

解决方案


毕竟,我的问题是如何在向下滚动时关闭对话框?

我猜你想在 ScrollView 滚动结束时关闭 PopupPage。如果是,请查看以下代码:

<pages:PopupPage
x:Class="FormsSample.simplecontrol.Page16"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup">
<StackLayout
    Padding="20,0"
    HorizontalOptions="FillAndExpand"
    VerticalOptions="Center">

    <Frame
        Padding="0"
        BackgroundColor="CadetBlue"
        CornerRadius="10"
        HeightRequest="100">
        <ScrollView Scrolled="ScrollView_Scrolled">
            <StackLayout Padding="10">
                <Label
                    FontSize="20"
                    HorizontalOptions="Center"
                    Text="First Popup Page"
                    TextColor="Black" />
                <Label Text="Hello Xamarin Guys" TextColor="Red" />

                <Label Text="This is Very Awesome Popup Plugins For Xamarin forms" TextColor="LightBlue" />
                <Button Text="Close" TextColor="Black" />
                <Image Source="a11.jpg" />
            </StackLayout>
        </ScrollView>
    </Frame>

</StackLayout></pages:PopupPage>

 private async void ScrollView_Scrolled(object sender, ScrolledEventArgs e)
    {
        ScrollView scrollView = sender as ScrollView;
        double scrollingSpace = scrollView.ContentSize.Height - scrollView.Height;

        if (scrollingSpace <= e.ScrollY) // Touched bottom
            // Do the things you want to do
            await PopupNavigation.Instance.PopAsync();
    }

推荐阅读