Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Force off-main-thread ItemsSource updates to update on main thread (#…
Browse files Browse the repository at this point in the history
…11235)

* Fixed CollectionView issue adding data in different thread on Android

* Fixed build error

* Changes to fix the build

* Force off-main-thread ObservableCollection changes to marshal to main thread

* Update Xamarin.Forms.Controls.Issues.Shared.projitems

* Update interface

* Restore old method

Co-authored-by: Javier Suárez Ruiz <javiersuarezruiz@hotmail.com>
Co-authored-by: Samantha Houts <samhouts@users.noreply.github.com>
Co-authored-by: Rui Marinho <me@ruimarinho.net>

fixes #10735
fixes #9753
  • Loading branch information
hartez committed Jul 20, 2020
1 parent 29ba746 commit ab55162
Show file tree
Hide file tree
Showing 11 changed files with 830 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .Xamarin.Forms.Android.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"solution": {
"path": "Xamarin.Forms.sln",
"projects": [
"PagesGallery\\PagesGallery.Droid\\PagesGallery.Droid.csproj",
"PagesGallery\\PagesGallery\\PagesGallery.csproj",
"Stubs\\Xamarin.Forms.Platform.Android\\Xamarin.Forms.Platform.Android (Forwarders).csproj",
"XFCorePostProcessor.Tasks\\XFCorePostProcessor.Tasks.csproj",
"Xamarin.Flex\\Xamarin.Flex.shproj",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8" ?>
<controls:TestContentPage
xmlns:controls="clr-namespace:Xamarin.Forms.Controls"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Xamarin.Forms.Controls.Issue10735"
Title="Issue 10735">
<StackLayout>
<Label
Padding="12"
BackgroundColor="Black"
TextColor="White"
Text="If this sample works without exceptions, the test has passed."/>
<CollectionView
x:Name="_collectionView"
ItemsSource="{Binding Items}"
VerticalOptions="Fill"
ItemSizingStrategy="MeasureAllItems"
ItemsUpdatingScrollMode="KeepLastItemInView">
<CollectionView.ItemTemplate>
<DataTemplate>
<Label
Text="{Binding}"
HorizontalOptions="Center"
VerticalOptions="Center"
FontSize="30" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

<StackLayout
VerticalOptions="End"
Orientation="Horizontal">
<Editor
x:Name="_editor"
HorizontalOptions="CenterAndExpand"
AutoSize="TextChanges" />
<Button
x:Name="_button"
HorizontalOptions="CenterAndExpand"/>
</StackLayout>
</StackLayout>
</controls:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Xamarin.Forms.Internals;
using Xamarin.Forms.CustomAttributes;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls
{
#if UITEST
[Category(UITestCategories.CollectionView)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 10735, "[Bug] [Fatal] [Android] CollectionView Causes Application Crash When Keyboard Opens", PlatformAffected.Android)]
public partial class Issue10735 : TestContentPage
{
readonly int _addItemDelay = 300;
int _item = 0;
#if APP
readonly int _changeFocusDelay = 1000;
View _lastFocus;
#endif

public Issue10735()
{
#if APP
InitializeComponent();
BindingContext = this;
StartAddingMessages();
#endif
}

public ObservableCollection<string> Items { get; } = new ObservableCollection<string>();

protected override void Init()
{

}

void StartAddingMessages()
{
Task.Run(async () =>
{
while (true)
{
await Task.Delay(_addItemDelay);
Items.Add(_item.ToString());
_item++;
}
});
#if APP
Task.Run(async () =>
{
while (true)
{
await Task.Delay(_changeFocusDelay);
Device.BeginInvokeOnMainThread(() =>
{
_lastFocus?.Unfocus();
if (_lastFocus == _editor)
_lastFocus = _button;
else
_lastFocus = _editor;
_lastFocus.Focus();
});
}
});
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1405,10 +1405,14 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue10530.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue7780.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8958.xaml.cs" />

<Compile Include="$(MSBuildThisFileDirectory)Issue10735.xaml.cs" />

<Compile Include="$(MSBuildThisFileDirectory)Issue10497.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10477.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10875.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10708.cs" />

<Compile Include="$(MSBuildThisFileDirectory)Issue9711.xaml.cs">
<DependentUpon>Issue9711.xaml</DependentUpon>
<SubType>Code</SubType>
Expand Down Expand Up @@ -1654,6 +1658,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue8958.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue10735.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue10477.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
Expand Down
Loading

0 comments on commit ab55162

Please sign in to comment.