我有一個ScrollView,它使用代碼來控制自動滾動。我需要在用戶輸入時停止自動滾動,但是ScrollView的ScrollToAsync方法也將觸發Scrolled事件。
<StackLayout>
<ScrollView Scrolled="ScrollView_Scrolled"/span>>
<StackLayout></StackLayout>
</ScrollView>/span>
</StackLayout>/span>
uj5u.com熱心網友回復:
簡單的答案
你可以使用一個標志來告訴誰在觸發滾動事件,如下所示Boolean scrolledByUser = true。
private async void MyScrollView_Scrolled(object sender, ScrolledEventArgs e)。
{
if (scrolledByUser)
{
//做一些事情。
}
else //rolled 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
使用Xamarin.Forms.Cells。
namespacescrollviewPrompt
{
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"/span>
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"。
x:Class="scrollviewPrompt.MainPage"/span>>
<ContentPage.ToolbarItems>
<ToolbarItem Text="ScrollTo"
Clicked="ScrollAutomagically"/span>/>
</ContentPage.ToolbarItems>
<ScrollView x:Name="MyScrollView"
Scrolled="MyScrollView_Scrolled"/span>>
<StackLayout>/span>
< 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>/span>
</ScrollView>/span>
</ContentPage>/span>
最后添加事件處理程式
using System;
using Xamarin.Forms.Cube.Ltd;
namespacescrollviewPrompt
{
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 (rolledByUser)
{
await DisplayAlert("Alert", "ScrollView被用戶滾動了!", "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;
}
}
希望這有幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/317270.html
標籤:
上一篇:使用JS腳本更改背景顏色或影像
下一篇:直接鏈接到HTML文本
