如何让ScrollView的Scrolled事件只响应Xamarin.Forms内的用户操作?
How to make ScrollView's Scrolled event only respond to user actions in Xamarin.Forms?
我有一个 ScrollView,它使用代码来控制自动滚动。我需要在用户输入时停止自动滚动,但是 ScrollView 的 ScrollToAsync
方法也会触发 Scrolled
事件。
<StackLayout>
<ScrollView Scrolled="ScrollView_Scrolled">
<StackLayout></StackLayout>
</ScrollView>
</StackLayout>
简答
您可以使用标志来告诉事件触发滚动事件的事件,如下所示
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 和很多 BoxViews 垂直,一个在另一个之上:
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;
}
}
}
希望对您有所帮助!
我有一个 ScrollView,它使用代码来控制自动滚动。我需要在用户输入时停止自动滚动,但是 ScrollView 的 ScrollToAsync
方法也会触发 Scrolled
事件。
<StackLayout>
<ScrollView Scrolled="ScrollView_Scrolled">
<StackLayout></StackLayout>
</ScrollView>
</StackLayout>
简答
您可以使用标志来告诉事件触发滚动事件的事件,如下所示
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 和很多 BoxViews 垂直,一个在另一个之上:
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;
}
}
}
希望对您有所帮助!