Skip to content
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

Strange Error... ObservableCollection with IGrouping on IOS (Development with Visual Studio Windows) #88

Open
reepmeDeveloper opened this issue Jun 21, 2021 · 0 comments

Comments

@reepmeDeveloper
Copy link

I noticed, that my app didn't worked right on iPhone.

So here is what I did to reproduce the error and make it hopefully noticeable to the Developer :-)

Starting with a new Xamarin Forms Project in Visual Studio (newest from every version)
Get the Refractored.MvvmHelpers 1.6.2 and inlcude it only in the base project.

The only File you have to create is that TestItem.cs inside the Models-Folder
You can simply copy and paste and might notice, that I have one Observable Collection bind to an CollectionView, which is working and one ObservableCollection with IGrouping bind to an CollectionView, which does trouble.

The Fun-Thing is... if you are doing a clear and a fill not in one method, it will work.

Models:
TestItem.cs --> public class TestItem
{
public string TestText { get; set; }

    public string NameSort
    {
        get
        {
            if (string.IsNullOrWhiteSpace(TestText) || TestText.Length == 0)
                return "?";

            return TestText[0].ToString().ToUpper();
        }
    }
}

ViewModel
AboutViewModel.cs -->
using ErrorTesting.Models;
using MvvmHelpers;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace ErrorTesting.ViewModels
{
public class AboutViewModel : BaseViewModel
{
public ObservableCollection TestCollection { get; set; }
public ObservableRangeCollection<IGrouping<string, TestItem>> GefilterteUndGruppierteObjekte { get; set; }

    public AboutViewModel()
    {
        TestCollection = new ObservableCollection<TestItem>();
        GefilterteUndGruppierteObjekte = new ObservableRangeCollection<IGrouping<string, TestItem>>();
        Title = "About";
        OpenWebCommand = new Command(async () => await Browser.OpenAsync("https://aka.ms/xamarin-quickstart"));
        DeleteElementCommand = new Command(LoescheDatensatz);
        ClearElementCommand = new Command(ClearDatensatz);
        FillElementCommand = new Command(FillDatensatz);
        FehlerElementCommand = new Command(FehlerDatensatz);
        DeleteTestElementCommand = new Command(LoescheTestDatensatz);
        ClearTestElementCommand = new Command(ClearTestDatensatz);
        FillTestElementCommand = new Command(FillTestDatensatz);
        FehlerTestElementCommand = new Command(FehlerTestDatensatz);
        ErzeugeTestdaten();
        ErzeugeDasIGrouping();
    }

    public ICommand OpenWebCommand { get; }
    public ICommand DeleteElementCommand { get; }
    public ICommand ClearElementCommand { get; }
    public ICommand FillElementCommand { get; }
    public ICommand FehlerElementCommand { get; }
    public ICommand DeleteTestElementCommand { get; }
    public ICommand ClearTestElementCommand { get; }
    public ICommand FillTestElementCommand { get; }
    public ICommand FehlerTestElementCommand { get; }

    private void ErzeugeTestdaten()
    {
        TestCollection.Add(new TestItem { TestText = "Erstes Element" });
        TestCollection.Add(new TestItem { TestText = "Zweites Element" });
        TestCollection.Add(new TestItem { TestText = "Drittes Element" });
        TestCollection.Add(new TestItem { TestText = "Viertes Element" });
    }

    private void LoescheTestDatensatz()
    {
        if (TestCollection.Count == 0)
            return;

        TestCollection.RemoveAt(0);
    }

    private void ClearTestDatensatz()
    {
        TestCollection.Clear();
    }

    private void FillTestDatensatz()
    {
        ErzeugeTestdaten();
    }


    private void FehlerTestDatensatz()
    {
        TestCollection.Clear();
        ErzeugeTestdaten();
    }

    private void LoescheDatensatz()
    {
        if (GefilterteUndGruppierteObjekte.Count == 0)
            return;

        GefilterteUndGruppierteObjekte.RemoveAt(0);
    }

    private void ClearDatensatz()
    {
        GefilterteUndGruppierteObjekte.Clear();
    }

    private void FillDatensatz()
    {
        ErzeugeDasIGrouping();
    }

    private void FehlerDatensatz()
    {
        GefilterteUndGruppierteObjekte.Clear();
        ErzeugeDasIGrouping();
    }

    public void ErzeugeDasIGrouping()
    {
        var items = TestCollection.GroupBy(gefiltertesObjekt => gefiltertesObjekt.NameSort);
        GefilterteUndGruppierteObjekte.AddRange(items);
    }
}

}

And Last the View
AboutPage.xml -->

<ContentPage.BindingContext>
    <vm:AboutViewModel />
</ContentPage.BindingContext>

<ContentPage.Resources>
    <ResourceDictionary>
        <Color x:Key="Accent">#96d1ff</Color>
    </ResourceDictionary>
</ContentPage.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <StackLayout Grid.Row="0" Orientation="Horizontal">
        <Button
                    Margin="20,0"
                    Command="{Binding DeleteTestElementCommand}"
                    Text="Entferne ein Element" />
        <Button
                    Margin="20,0"
                    Command="{Binding ClearTestElementCommand}"
                    Text="Clear" />
        <Button
                    Margin="20,0"
                    Command="{Binding FillTestElementCommand}"
                    Text="Fill" />
        <Button
                    Margin="20,0"
                    Command="{Binding FehlerTestElementCommand}"
                    Text="Fehler" />
    </StackLayout>
    <CollectionView
                Grid.Row="1"
                Margin="20,0"
                ItemsSource="{Binding TestCollection}"
                SelectionMode="Single">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Label Grid.Row="0">Alles klar</Label>
                    <Label Grid.Row="1" Text="{Binding TestText}" />
                    <Label Grid.Row="2">...</Label>
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
    <StackLayout Grid.Row="2" Orientation="Horizontal">
        <Button
                    Margin="20,0"
                    Command="{Binding DeleteElementCommand}"
                    Text="Entferne ein Element" />
        <Button
                    Margin="20,0"
                    Command="{Binding ClearElementCommand}"
                    Text="Clear" />
        <Button
                    Margin="20,0"
                    Command="{Binding FillElementCommand}"
                    Text="Fill" />
        <Button
                    Margin="20,0"
                    Command="{Binding FehlerElementCommand}"
                    Text="Fehler" />
    </StackLayout>
    <CollectionView
                Grid.Row="3"
                Margin="20,0"
                IsGrouped="True"
                ItemsSource="{Binding GefilterteUndGruppierteObjekte}"
                SelectionMode="Single">
        <CollectionView.GroupHeaderTemplate>
            <DataTemplate>
                <Label
                            Margin="0,0,0,0"
                            BackgroundColor="LightGray"
                            FontAttributes="Bold"
                            FontSize="Large"
                            Text="{Binding Key}" />
            </DataTemplate>
        </CollectionView.GroupHeaderTemplate>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Label Grid.Row="0">Alles klar</Label>
                    <Label Grid.Row="1" Text="{Binding TestText}" />
                    <Label Grid.Row="2">...</Label>
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</Grid>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant