forked from dotnet/maui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Windows] Fix crash using ScrollTo on CollectionView (dotnet#19921)
* Fix 17865 * Added device test * Updated test * Fixup use of event * Update MauiProgram.cs * Fix UI test --------- Co-authored-by: Mike Corsaro <[email protected]> Co-authored-by: Mike Corsaro <[email protected]> Co-authored-by: Gerald Versluis <[email protected]>
- Loading branch information
1 parent
334866f
commit 1385a01
Showing
5 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Controls/tests/TestCases.HostApp/Issues/Issue17865.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
x:Class="Maui.Controls.Sample.Issues.Issue17865" | ||
xmlns:ns="clr-namespace:Maui.Controls.Sample.Issues" | ||
Title="Issue 17865"> | ||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition | ||
Height="Auto"/> | ||
<RowDefinition | ||
Height="*"/> | ||
</Grid.RowDefinitions> | ||
<HorizontalStackLayout | ||
Padding="5"> | ||
<Button | ||
AutomationId="WaitForStubControl" | ||
Text="Reveal Last Item" | ||
Clicked="OnButtonClicked"/> | ||
</HorizontalStackLayout> | ||
<CollectionView | ||
x:Name="collectionView" | ||
Grid.Row="1" | ||
ItemsSource="{Binding Items}"> | ||
<CollectionView.ItemTemplate> | ||
<DataTemplate> | ||
<Label | ||
Margin="5,2" | ||
Text="{Binding ItemText}" | ||
HorizontalOptions="Fill" | ||
TextColor="Gray"/> | ||
</DataTemplate> | ||
</CollectionView.ItemTemplate> | ||
</CollectionView> | ||
</Grid> | ||
|
||
</ContentPage> |
71 changes: 71 additions & 0 deletions
71
src/Controls/tests/TestCases.HostApp/Issues/Issue17865.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using Microsoft.Maui.Controls; | ||
using Microsoft.Maui.Controls.Xaml; | ||
|
||
namespace Maui.Controls.Sample.Issues | ||
{ | ||
internal class Issue17865Model | ||
{ | ||
public Issue17865Model(int itemIndex) | ||
{ | ||
ItemText = $"Item #{itemIndex}"; | ||
} | ||
|
||
public string ItemText { get; } | ||
|
||
} | ||
|
||
internal class Issue17865ViewModel : BindableObject | ||
{ | ||
ObservableCollection<Issue17865Model> items = new(); | ||
|
||
public Issue17865ViewModel() | ||
{ | ||
Populate(); | ||
} | ||
|
||
void Populate() | ||
{ | ||
for (int i = 0; i < 100; i++) | ||
{ | ||
items.Add(new Issue17865Model(i)); | ||
} | ||
} | ||
|
||
public ObservableCollection<Issue17865Model> Items => items; | ||
} | ||
|
||
[XamlCompilation(XamlCompilationOptions.Compile)] | ||
[Issue(IssueTracker.Github, 17865, "CollectionView throws NRE when ScrollTo method is called from a handler of event Window.Created", PlatformAffected.UWP)] | ||
public partial class Issue17865 : ContentPage | ||
{ | ||
readonly Issue17865ViewModel _viewModel; | ||
|
||
public Issue17865() | ||
{ | ||
InitializeComponent(); | ||
|
||
collectionView.Loaded += CollectionView_Loaded; | ||
|
||
BindingContext = _viewModel = new Issue17865ViewModel(); | ||
} | ||
|
||
private void CollectionView_Loaded(object sender, EventArgs e) | ||
{ | ||
RevealLastItem(); | ||
} | ||
|
||
public void RevealLastItem() | ||
{ | ||
var item = _viewModel.Items.Last(); | ||
collectionView.ScrollTo(item, null, ScrollToPosition.MakeVisible, false); | ||
} | ||
|
||
private void OnButtonClicked(object sender, EventArgs e) | ||
{ | ||
RevealLastItem(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,4 +96,4 @@ protected override Window CreateWindow(IActivationState activationState) | |
} | ||
#endif | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue17865.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#if WINDOWS | ||
using NUnit.Framework; | ||
using UITest.Appium; | ||
using UITest.Core; | ||
|
||
namespace Microsoft.Maui.TestCases.Tests.Issues | ||
{ | ||
public class Issue17865 : _IssuesUITest | ||
{ | ||
const string ButtonId = "WaitForStubControl"; | ||
|
||
public Issue17865(TestDevice device) : base(device) { } | ||
|
||
public override string Issue => "CollectionView throws NRE when ScrollTo method is called from a handler of event Window.Created"; | ||
|
||
[Test] | ||
[Category(UITestCategories.CollectionView)] | ||
public void Issue17865Test() | ||
{ | ||
App.WaitForElement(ButtonId); | ||
|
||
App.Click(ButtonId); | ||
|
||
// NOTE: Without crashes the test has passed. | ||
} | ||
} | ||
} | ||
#endif |