Issue
I have in my code behind file made an override to the OnAppearing() method, to do some automatic scrolling to a specific element which is a child inside my ScrollView element in my XAML file. I use the method ScrollToAsync()
from the ScrollView element, to Scroll to the element MyBox, which is a BoxView element.
protected override async void OnAppearing()
{
await HorizontalScrollView.ScrollToAsync(MyBox, ScrollToPosition.Center, false);
base.OnAppearing();
}
My Problem is here that when the page appear it doesnt scroll to the MyBox element automatically. However I got it to work if I inserted a Task.Delay()
.
protected override async void OnAppearing()
{
await System.Threading.Tasks.Task.Delay(1);
await HorizontalScrollView.ScrollToAsync(MyBox, ScrollToPosition.Center, false);
base.OnAppearing();
}
My question is why it works, when I insert the delay, and is there any way I can get rid of using the Task.Delay()
statement?
Solution
You need to invoke the line in UIThread (Main Thread) like following
protected override async void OnAppearing()
{
base.OnAppearing();
Device.BeginInvokeOnMainThread(async ()=> {
await HorizontalScrollView.ScrollToAsync(MyBox, ScrollToPosition.Center, false);
});
}
Answered By - Lucas Zhang - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.