Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Xunit;
#if IOS || MACCATALYST
using NavigationViewHandler = Microsoft.Maui.Controls.Handlers.Compatibility.NavigationRenderer;
#endif

namespace Microsoft.Maui.DeviceTests
{
Expand Down Expand Up @@ -196,12 +199,89 @@ await CreateHandlerAndAddToWindow(parentLayout, () =>
Assert.Equal(expectedHeight, parentLayout.Height, 2.0);
}

[Fact(DisplayName = "Test call ScrollToAsync within NavigatedTo event")]
public async Task TestCallScrollToAsyncWithinNavigatedToEvent()
{
SetupBuilder();
var page1 = new ContentPage();
var page2 = new ContentPage();
var page3 = new ContentPage();
var scrollView = new ScrollView()
{
Orientation = ScrollOrientation.Vertical,
Content = new Grid
{
WidthRequest = 300,
HeightRequest = 1000
}
};
page2.Content = scrollView;
page2.NavigatedTo += async (_, __) =>
{
double expectedPositionX = 0;
double expectedPositionY = 200;
await scrollView.ScrollToAsync(expectedPositionX, expectedPositionY, true);
Assert.Equal(expectedPositionX, scrollView.ScrollX);
Assert.Equal(expectedPositionY, scrollView.ScrollY);
};
var navPage = new NavigationPage(page1);

await CreateHandlerAndAddToWindow<NavigationViewHandler>(navPage, async (handler) =>
{
await page1.Navigation.PushAsync(page2);
await page2.Navigation.PushAsync(page3);
await page3.Navigation.PopAsync();
});
}

[Fact(DisplayName = "Test call ScrollToAsync within Appearing event")]
public async Task TestCallScrollToAsyncWithinAppearingEvent()
{
SetupBuilder();
var page1 = new ContentPage();
var page2 = new ContentPage();
var page3 = new ContentPage();
var scrollView = new ScrollView()
{
Orientation = ScrollOrientation.Vertical,
Content = new Grid
{
WidthRequest = 300,
HeightRequest = 1000
}
};
page2.Content = scrollView;
page2.Appearing += async (_, __) =>
{
double expectedPositionX = 0;
double expectedPositionY = 200;
await scrollView.ScrollToAsync(expectedPositionX, expectedPositionY, true);
Assert.Equal(expectedPositionX, scrollView.ScrollX);
Assert.Equal(expectedPositionY, scrollView.ScrollY);
};
var navPage = new NavigationPage(page1);

await CreateHandlerAndAddToWindow<NavigationViewHandler>(navPage, async (handler) =>
{
await page1.Navigation.PushAsync(page2);
await page2.Navigation.PushAsync(page3);
await page3.Navigation.PopAsync();
});
}

void SetupBuilder()
{
EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(Toolbar), typeof(ToolbarHandler));
#if IOS || MACCATALYST
handlers.AddHandler(typeof(NavigationPage), typeof(NavigationViewHandler));
#else
handlers.AddHandler(typeof(NavigationPage), typeof(NavigationViewHandler));
#endif
handlers.AddHandler<Page, PageHandler>();
handlers.AddHandler<Label, LabelHandler>();
handlers.AddHandler<IScrollView, ScrollViewHandler>();
handlers.AddHandler<Grid, LayoutHandler>();
Expand Down
10 changes: 9 additions & 1 deletion src/Core/src/Handlers/ScrollView/ScrollViewHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ public static void MapRequestScrollTo(IScrollViewHandler handler, IScrollView sc
{
if (args is ScrollToRequest request)
{
handler.PlatformView.ChangeView(request.HorizontalOffset, request.VerticalOffset, null, request.Instant);
if (handler.PlatformView.HorizontalOffset == request.HorizontalOffset &&
handler.PlatformView.VerticalOffset == request.VerticalOffset)
{
handler.VirtualView.ScrollFinished();
}
else
{
handler.PlatformView.ChangeView(request.HorizontalOffset, request.VerticalOffset, null, request.Instant);
}
}
}

Expand Down
Loading