【问题标题】:How to make ScrollView's Scrolled event only respond to user actions in Xamarin.Forms?如何让 ScrollView 的 Scrolled 事件只响应 Xamarin.Forms 中的用户操作?
【发布时间】:2021-09-16 05:02:53
【问题描述】:

我有一个 ScrollView,它使用代码来控制自动滚动。我需要在用户输入时停止自动滚动,但是 ScrollViewScrollToAsync 方法也会触发Scrolled 事件。

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

【问题讨论】:

    标签: c# xamarin xamarin.forms scrollview


    【解决方案1】:

    简短的回答

    您可以使用一个标志来告诉事件是谁触发了滚动事件,如下所示

    Boolean scrolledByUser = true;
    
    private async void MyScrollView_Scrolled(object sender, ScrolledEventArgs e)
    {
      if (scrolledByUser)
      {
        // do something
      }
      else // scrolled automagically
      {
        // do something else
      }
    }
    
    private async void ScrollAutomagically(object sender, EventArgs e)
    {
      scrolledByUser = false;
      await MyScrollView.ScrollToAsync(ScrollView.X, 800, true);
      scrolledByUser = true;
    }
    

    长答案

    接下来你可以找到一个完整的示例来说明我的建议。

    您从一个新的空白项目开始,然后修改 App.xaml.cs 使其看起来像

    App.xaml.cs

    using Xamarin.Forms;
    
    namespace scrollviewPrompt
    {
        public partial class App : Application
        {
            public App()
            {
                InitializeComponent();
    
                MainPage = new NavigationPage(new MainPage());
            }
        }
    }
    

    然后继续修改 MainPage.xaml 以包含一个 ScrollView,其中包含许多垂直的 BoxView,一个在另一个之上: p>

    MainPage.xaml

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="scrollviewPrompt.MainPage">
    
        <ContentPage.ToolbarItems>
            <ToolbarItem Text="ScrollTo"
                         Clicked="ScrollAutomagically"/>
        </ContentPage.ToolbarItems>
    
        <ScrollView x:Name="MyScrollView" 
                        Scrolled="MyScrollView_Scrolled">
            <StackLayout>
                <BoxView BackgroundColor="Red"         HeightRequest="128"/>
                <BoxView BackgroundColor="Orange"      HeightRequest="128"/>
                <BoxView BackgroundColor="Blue"      HeightRequest="128"/>
                <BoxView BackgroundColor="Green"      HeightRequest="128"/>
                <BoxView BackgroundColor="Black"      HeightRequest="128"/>
                <BoxView BackgroundColor="AliceBlue"         HeightRequest="128"/>
                <BoxView BackgroundColor="Brown"      HeightRequest="128"/>
                <BoxView BackgroundColor="Pink"      HeightRequest="128"/>
                <BoxView BackgroundColor="White"      HeightRequest="128"/>
                <BoxView BackgroundColor="Yellow"      HeightRequest="128"/>
                <BoxView BackgroundColor="Red"         HeightRequest="128"/>
                <BoxView BackgroundColor="Orange"      HeightRequest="128"/>
                <BoxView BackgroundColor="Blue"      HeightRequest="128"/>
                <BoxView BackgroundColor="Green"      HeightRequest="128"/>
                <BoxView BackgroundColor="Black"      HeightRequest="128"/>
                <BoxView BackgroundColor="AliceBlue"         HeightRequest="128"/>
                <BoxView BackgroundColor="Brown"      HeightRequest="128"/>
                <BoxView BackgroundColor="Pink"      HeightRequest="128"/>
                <BoxView BackgroundColor="White"      HeightRequest="128"/>
                <BoxView BackgroundColor="Yellow"      HeightRequest="128"/>
                <BoxView BackgroundColor="Red"         HeightRequest="128"/>
                <BoxView BackgroundColor="Orange"      HeightRequest="128"/>
                <BoxView BackgroundColor="Blue"      HeightRequest="128"/>
                <BoxView BackgroundColor="Green"      HeightRequest="128"/>
                <BoxView BackgroundColor="Black"      HeightRequest="128"/>
                <BoxView BackgroundColor="AliceBlue"         HeightRequest="128"/>
                <BoxView BackgroundColor="Brown"      HeightRequest="128"/>
                <BoxView BackgroundColor="Pink"      HeightRequest="128"/>
                <BoxView BackgroundColor="White"      HeightRequest="128"/>
                <BoxView BackgroundColor="Yellow"      HeightRequest="128"/>
                <BoxView BackgroundColor="Red"         HeightRequest="128"/>
                <BoxView BackgroundColor="Orange"      HeightRequest="128"/>
                <BoxView BackgroundColor="Blue"      HeightRequest="128"/>
                <BoxView BackgroundColor="Green"      HeightRequest="128"/>
                <BoxView BackgroundColor="Black"      HeightRequest="128"/>
                <BoxView BackgroundColor="AliceBlue"         HeightRequest="128"/>
                <BoxView BackgroundColor="Brown"      HeightRequest="128"/>
                <BoxView BackgroundColor="Pink"      HeightRequest="128"/>
                <BoxView BackgroundColor="White"      HeightRequest="128"/>
                <BoxView BackgroundColor="Yellow"      HeightRequest="128"/>
                <BoxView BackgroundColor="Red"         HeightRequest="128"/>
                <BoxView BackgroundColor="Orange"      HeightRequest="128"/>
                <BoxView BackgroundColor="Blue"      HeightRequest="128"/>
                <BoxView BackgroundColor="Green"      HeightRequest="128"/>
                <BoxView BackgroundColor="Black"      HeightRequest="128"/>
                <BoxView BackgroundColor="AliceBlue"         HeightRequest="128"/>
                <BoxView BackgroundColor="Brown"      HeightRequest="128"/>
                <BoxView BackgroundColor="Pink"      HeightRequest="128"/>
                <BoxView BackgroundColor="White"      HeightRequest="128"/>
                <BoxView BackgroundColor="Yellow"      HeightRequest="128"/>
                <BoxView BackgroundColor="Red"         HeightRequest="128"/>
                <BoxView BackgroundColor="Orange"      HeightRequest="128"/>
                <BoxView BackgroundColor="Blue"      HeightRequest="128"/>
                <BoxView BackgroundColor="Green"      HeightRequest="128"/>
                <BoxView BackgroundColor="Black"      HeightRequest="128"/>
                <BoxView BackgroundColor="AliceBlue"         HeightRequest="128"/>
                <BoxView BackgroundColor="Brown"      HeightRequest="128"/>
                <BoxView BackgroundColor="Pink"      HeightRequest="128"/>
                <BoxView BackgroundColor="White"      HeightRequest="128"/>
                <BoxView BackgroundColor="Yellow"      HeightRequest="128"/>
                <BoxView BackgroundColor="Red"         HeightRequest="128"/>
                <BoxView BackgroundColor="Orange"      HeightRequest="128"/>
                <BoxView BackgroundColor="Blue"      HeightRequest="128"/>
                <BoxView BackgroundColor="Green"      HeightRequest="128"/>
                <BoxView BackgroundColor="Black"      HeightRequest="128"/>
                <BoxView BackgroundColor="AliceBlue"         HeightRequest="128"/>
                <BoxView BackgroundColor="Brown"      HeightRequest="128"/>
                <BoxView BackgroundColor="Pink"      HeightRequest="128"/>
                <BoxView BackgroundColor="White"      HeightRequest="128"/>
                <BoxView BackgroundColor="Yellow"      HeightRequest="128"/>
                <BoxView BackgroundColor="Red"         HeightRequest="128"/>
                <BoxView BackgroundColor="Orange"      HeightRequest="128"/>
                <BoxView BackgroundColor="Blue"      HeightRequest="128"/>
                <BoxView BackgroundColor="Green"      HeightRequest="128"/>
                <BoxView BackgroundColor="Black"      HeightRequest="128"/>
                <BoxView BackgroundColor="AliceBlue"         HeightRequest="128"/>
                <BoxView BackgroundColor="Brown"      HeightRequest="128"/>
                <BoxView BackgroundColor="Pink"      HeightRequest="128"/>
                <BoxView BackgroundColor="White"      HeightRequest="128"/>
                <BoxView BackgroundColor="Yellow"      HeightRequest="128"/>
                <BoxView BackgroundColor="Red"         HeightRequest="128"/>
                <BoxView BackgroundColor="Orange"      HeightRequest="128"/>
                <BoxView BackgroundColor="Blue"      HeightRequest="128"/>
                <BoxView BackgroundColor="Green"      HeightRequest="128"/>
                <BoxView BackgroundColor="Black"      HeightRequest="128"/>
                <BoxView BackgroundColor="AliceBlue"         HeightRequest="128"/>
                <BoxView BackgroundColor="Brown"      HeightRequest="128"/>
                <BoxView BackgroundColor="Pink"      HeightRequest="128"/>
                <BoxView BackgroundColor="White"      HeightRequest="128"/>
                <BoxView BackgroundColor="Yellow"      HeightRequest="128"/>
                <BoxView BackgroundColor="Red"         HeightRequest="128"/>
                <BoxView BackgroundColor="Orange"      HeightRequest="128"/>
                <BoxView BackgroundColor="Blue"      HeightRequest="128"/>
                <BoxView BackgroundColor="Green"      HeightRequest="128"/>
                <BoxView BackgroundColor="Black"      HeightRequest="128"/>
                <BoxView BackgroundColor="AliceBlue"         HeightRequest="128"/>
                <BoxView BackgroundColor="Brown"      HeightRequest="128"/>
                <BoxView BackgroundColor="Pink"      HeightRequest="128"/>
                <BoxView BackgroundColor="White"      HeightRequest="128"/>
                <BoxView BackgroundColor="Yellow"      HeightRequest="128"/>
            </StackLayout>
        </ScrollView>
    </ContentPage>
    

    最后添加事件处理程序

    using System;
    using Xamarin.Forms;
    
    namespace scrollviewPrompt
    {
        public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
            }
    
            Boolean handled = false;
    
            Boolean scrolledByUser = true;
    
            private async void MyScrollView_Scrolled(object sender, ScrolledEventArgs e)
            {
                if (!handled)
                {
                    handled = true;
    
                    if (scrolledByUser)
                    {
                        await DisplayAlert("Alert", "ScrollView Scrolled by user!", "Ok");
                    }
                    else
                    {
                        await DisplayAlert("Alert", "ScrollView Scrolled automagically!", "Ok");
                    }
                    handled = false;
                }
            }
    
            private async void ScrollAutomagically(object sender, EventArgs e)
            {
                scrolledByUser = false;
                await MyScrollView.ScrollToAsync(MyScrollView.X, 800, true);
                scrolledByUser = true;
            }
        }
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 2014-05-10
      • 1970-01-01
      相关资源
      最近更新 更多