We have an instant message app. We want to scroll automatically when a message is received so the new message is visible. Currently, when the first message that requires scrolling is received, the page does not scroll. That is, the new message is hidden behind our input controls. It requires another message or the user to adjust the scrollview and then it works properly.
Our xaml is something like this:
<ContentPage>
<StackLayout x:Name="mainStackLayout">
<customControls:NavBar />
<ScrollView x:Name="mainScrollView">
<StackLayout x:Name="mainScrollViewStackLayout">
<ScrollView x:Name="messagesScrollView">
<StackLayout x:Name="messagesScrollViewContentStackLayout">
<!-- Messages are programmatically inserted here -->
</StackLayout>
</ScrollView>
</StackLayout>
</ScrollView>
<StackLayout>
<!-- a couple buttons/inputs for sending messages -->
</StackLayout>
</StackLayout>
</ContentPage>
And the code we call to scroll looks like this:
public void ScrollMessagesToEnd()
{
StackLayout messagesContent = (messagesScrollView.Content as StackLayout);
var frame = messagesContent.Children.LastOrDefault();
if (frame != null)
{
messagesScrollView.ScrollToAsync(frame, ScrollToPosition.MakeVisible, true);
}
}