-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[iOS] Avoid crash adding items to ObservableCollection #21613
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/Controls/samples/Controls.Sample.UITests/Issues/Issue21374.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,47 @@ | ||
<?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.Issue21374" | ||
xmlns:viewModels="clr-namespace:Maui.Controls.Sample.Issues" | ||
Title="Issue 21374"> | ||
<ContentPage.BindingContext> | ||
<viewModels:Issue21374ViewModel /> | ||
</ContentPage.BindingContext> | ||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="*" /> | ||
</Grid.RowDefinitions> | ||
<Button | ||
Grid.Row="0" | ||
AutomationId="WaitForStubControl" | ||
Command="{Binding PopulateItemsCommand}" | ||
Text="Populate items" /> | ||
<Grid | ||
Grid.Row="1"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="*" /> | ||
<RowDefinition Height="Auto" /> | ||
</Grid.RowDefinitions> | ||
<CollectionView | ||
Grid.Row="0" | ||
ItemsSource="{Binding Items}"> | ||
<CollectionView.ItemTemplate> | ||
<DataTemplate> | ||
<StackLayout | ||
Padding="8" | ||
Orientation="Horizontal" | ||
Spacing="16"> | ||
<Label | ||
Text="{Binding Text}" /> | ||
</StackLayout> | ||
</DataTemplate> | ||
</CollectionView.ItemTemplate> | ||
</CollectionView> | ||
<Label | ||
Grid.Row="1" | ||
Text="{Binding Success}"/> | ||
</Grid> | ||
</Grid> | ||
</ContentPage> |
86 changes: 86 additions & 0 deletions
86
src/Controls/samples/Controls.Sample.UITests/Issues/Issue21374.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,86 @@ | ||
using System.Collections.ObjectModel; | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
using System.Windows.Input; | ||
using Microsoft.Maui.Controls; | ||
using Microsoft.Maui.Controls.Xaml; | ||
|
||
namespace Maui.Controls.Sample.Issues | ||
{ | ||
[XamlCompilation(XamlCompilationOptions.Compile)] | ||
[Issue(IssueTracker.Github, 21374, "Error when adding to ObservableCollection", PlatformAffected.iOS)] | ||
public partial class Issue21374 : ContentPage | ||
{ | ||
public Issue21374() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
|
||
public class Issue21374Model | ||
{ | ||
public string Text { get; set; } | ||
} | ||
|
||
public class Issue21374ViewModel : INotifyPropertyChanged | ||
{ | ||
string _success; | ||
ObservableCollection<Issue21374Model> _items = new ObservableCollection<Issue21374Model>(); | ||
|
||
public ICommand PopulateItemsCommand => new Command(PopulateItems); | ||
|
||
public string Success | ||
{ | ||
get => _success; | ||
set | ||
{ | ||
_success = value; | ||
OnPropertyChanged("Success"); | ||
} | ||
} | ||
|
||
public ObservableCollection<Issue21374Model> Items | ||
{ | ||
get => _items; | ||
set | ||
{ | ||
if (_items != value) | ||
{ | ||
_items = value; | ||
OnPropertyChanged("Items"); | ||
} | ||
} | ||
} | ||
|
||
public event PropertyChangedEventHandler PropertyChanged; | ||
|
||
void PopulateItems() | ||
{ | ||
try | ||
{ | ||
for (int j = 0; j < 10; j++) | ||
{ | ||
Items.Add(new Issue21374Model { Text = $"Item {j + 1}" }); | ||
} | ||
|
||
Success = "Success"; | ||
} | ||
catch | ||
{ | ||
Success = "Failed"; | ||
} | ||
} | ||
|
||
protected void OnPropertyChanged([CallerMemberName] string propertyName = "") | ||
{ | ||
PropertyChangedEventHandler changed = PropertyChanged; | ||
|
||
if (changed == null) | ||
{ | ||
return; | ||
} | ||
|
||
changed.Invoke(this, new PropertyChangedEventArgs(propertyName)); | ||
} | ||
} | ||
} |
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
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,31 @@ | ||
using NUnit.Framework; | ||
using UITest.Appium; | ||
using UITest.Core; | ||
|
||
namespace Microsoft.Maui.AppiumTests.Issues | ||
{ | ||
public class Issue21374 : _IssuesUITest | ||
{ | ||
public Issue21374(TestDevice device) : base(device) | ||
{ | ||
} | ||
|
||
public override string Issue => "Error when adding to ObservableCollection"; | ||
|
||
[Test] | ||
[Category(UITestCategories.CollectionView)] | ||
public void AddingItemsToObservableCollectionNoCrash() | ||
{ | ||
this.IgnoreIfPlatforms(new[] | ||
{ | ||
TestDevice.Android, | ||
TestDevice.Mac, | ||
TestDevice.Windows | ||
}); | ||
|
||
App.WaitForElement("WaitForStubControl"); | ||
App.Click("WaitForStubControl"); | ||
App.WaitForNoElement("Success"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Investigating why null is returned in unexpected cases (the cell is visible).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @PureWeen
Related to #21606