Skip to content

Commit

Permalink
[Windows] Fix CollectionView DisconnectHandler SelectionMode Crash (#…
Browse files Browse the repository at this point in the history
…23726)

* Reorder events to prevent disconnect issues

* Add test

---------

Co-authored-by: Mike Corsaro <mikecorsaro@microsoft.com>
  • Loading branch information
Foda and Mike Corsaro authored Jul 20, 2024
1 parent 9d56850 commit c8aa6f9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ protected override void DisconnectHandler(ListViewBase platformView)

if (oldListViewBase != null)
{
oldListViewBase.ClearValue(ListViewBase.SelectionModeProperty);
oldListViewBase.SelectionChanged -= PlatformSelectionChanged;
oldListViewBase.ClearValue(ListViewBase.SelectionModeProperty);
}

if (ItemsView != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
Expand Down Expand Up @@ -58,6 +59,51 @@ await CreateHandlerAndAddToWindow<LayoutHandler>(layout, (handler) =>
});
}

[Fact(DisplayName = "CollectionView Disconnects Correctly with MultiSelection")]
public async Task CollectionViewHandlerDisconnectsWithMultiSelect()
{
SetupBuilder();

ObservableCollection<string> data = new ObservableCollection<string>()
{
"Item 1",
"Item 2",
"Item 3"
};

var collectionView = new CollectionView()
{
ItemTemplate = new Controls.DataTemplate(() =>
{
return new VerticalStackLayout()
{
new Label()
};
}),
SelectionMode = SelectionMode.Multiple,
ItemsSource = data
};

var layout = new VerticalStackLayout()
{
collectionView
};

await CreateHandlerAndAddToWindow<LayoutHandler>(layout, (handler) =>
{
collectionView.SelectedItems.Add(data[0]);
collectionView.SelectedItems.Add(data[2]);

// Validate that no exceptions are thrown
var collectionViewHandler = (IElementHandler)collectionView.Handler;
collectionViewHandler.DisconnectHandler();

((IElementHandler)handler).DisconnectHandler();

return Task.CompletedTask;
});
}

[Fact]
public async Task ValidateItemContainerDefaultHeight()
{
Expand Down

0 comments on commit c8aa6f9

Please sign in to comment.