locked
XF5Prev4: CarouselView crashes RRS feed

  • Question

  • User386366 posted

    I have a page where the User can search for items to be shown in a CarouselView. I also have a Button to reset the search and clear the CarouselView. In XFPrev3 this reset led to an other Crash-Bug but this is fixed in Prev4 and is another story... So I can clear the Collection now, but when I refill the ObservableCollection again and trigger "OnPropertyChanged" to refresh my CarouselView the App crashes again. I made a sample app to demonstrate this. If I use the CollectionView instead, everything works fine, but I can not use the CollectionView due to other problems.

    Tuesday, November 17, 2020 8:00 AM

Answers

  • User386366 posted

    I finally found a solution ! When I just set the Property "Loop="True" then the CarouselView is not crashing anymore !!! I am so happy, although it is just a workaround !

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, November 19, 2020 9:00 AM

All replies

  • User379860 posted

    ObservableCollection have achieve the INotifyPropertyChanged, you do not need to execute again with OnPropertyChanged(nameof(data));. Here is editted code.

    ``` { public ObservableCollection data { get; set; } public Test SelectedItem { get; set; }

        private void FillCollection()
        {
           // data = null;
    
            data.Add(new Test { TestA = "PLEASE", TestB = 0 });
            data.Add(new Test { TestA = "FIX", TestB = 1 });
            data.Add(new Test { TestA = "THIS", TestB = 2 });
            data.Add(new Test { TestA = "PROBLEM !", TestB = 3 });
    
           // OnPropertyChanged(nameof(data));
        }
        public AboutViewModel()
        {
            data = new ObservableCollection<Test>();
            Title = "About";
            FillCollection();
            ClearCommand = new Command(Clear);
            RefillCommand = new Command(Refill);
        }
    
        private void Refill(object obj)
        {
            FillCollection();
        }
    
        private void Clear(object obj)
        {
            data.Clear();
        }
    
        public ICommand ClearCommand { get; }
        public ICommand RefillCommand { get; }
    }
    

    } ```

    Here is running GIF.

    Tuesday, November 17, 2020 10:37 AM
  • User386366 posted

    Thank you LeonLu. Yes, now this sample is working and you are right.

    The "OnPropertyChanged" was really not needed in this sample, but in my real Project, I load my Collection from a REST-API. And when I use e.g. MyCollection = ReadFromAPI() I get a new Collection and I have to Fire "OnPropertyChanged" to refresh my View. Without it is not refreshing.

    I tried now to load first my result into a Variable and add each Item separately, like so

    var result = ReadFromAPI(); foreach(var item in result) { MyCollection.Add(item); } But the View is also not refreshing without "OnPropertyChanged".

    Maybe I have a strange constellation in my project that nobody can reproduce but when I use a CollectionView it works fine so that I think that there is an error somewhere in the CarouselView.

    Maybe it is getting fixed in the next XF Update...

    @LeonLu said: ObservableCollection have achieve the INotifyPropertyChanged, you do not need to execute again with OnPropertyChanged(nameof(data));. Here is editted code.

    ``` { public ObservableCollection data { get; set; } public Test SelectedItem { get; set; }

        private void FillCollection()
        {
           // data = null;
    
            data.Add(new Test { TestA = "PLEASE", TestB = 0 });
            data.Add(new Test { TestA = "FIX", TestB = 1 });
            data.Add(new Test { TestA = "THIS", TestB = 2 });
            data.Add(new Test { TestA = "PROBLEM !", TestB = 3 });
    
           // OnPropertyChanged(nameof(data));
        }
        public AboutViewModel()
        {
            data = new ObservableCollection<Test>();
            Title = "About";
            FillCollection();
            ClearCommand = new Command(Clear);
            RefillCommand = new Command(Refill);
        }
    
        private void Refill(object obj)
        {
            FillCollection();
        }
    
        private void Clear(object obj)
        {
            data.Clear();
        }
    
        public ICommand ClearCommand { get; }
        public ICommand RefillCommand { get; }
    }
    

    } ```

    Here is running GIF.

    Tuesday, November 17, 2020 12:21 PM
  • User379860 posted

    Carouselview is preview version, So you will meet strange question when you use it.

    CarouselView shares much of its implementation with CollectionView. I recommand you to use CollectionView.

    Tuesday, November 17, 2020 12:39 PM
  • User386366 posted

    Thank you again for your time, I really appreciate it. Actually I am forced to use the CollectionView due to my problem, but I have two disadvantages now:

    1. I am only able to display only one item per page when I set a fixed width in my ItemTemplate. This does not work with changing resolutions

    2. the current Item is not automatically selected anymore. I am missing the "CurrentItem"-Property. Now the user has to tap at least one time on the page to make the displayed item to "his" one. That is dangerous because when he does not, a other item is maybe still selected and causes problems.

    If there is at least a solution for problem 2, I would be very happy....

    @LeonLu said: Carouselview is preview version, So you will meet strange question when you use it.

    CarouselView shares much of its implementation with CollectionView. I recommand you to use CollectionView.

    Tuesday, November 17, 2020 12:48 PM
  • User379860 posted

    the current Item is not automatically selected anymore. I am missing the "CurrentItem"-Property. Now the user has to tap at least one time on the page to make the displayed item to "his" one. That is dangerous because when he does not, a other item is maybe still selected and causes problems.

    Do you mean you want to achieve the pre-selection function like this topic? https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/selection#single-pre-selection

    Wednesday, November 18, 2020 9:27 AM
  • User386366 posted

    Yes and no. I want to display only one Item per page in my CollectionView (this goal I could manage). This one should be always automatically selected. I am looking for a Method to detect if an Item is visible on the screen. Then I could set my SelectedItem-Property to this very item. Do you see any chances ?

    @LeonLu said:

    the current Item is not automatically selected anymore. I am missing the "CurrentItem"-Property. Now the user has to tap at least one time on the page to make the displayed item to "his" one. That is dangerous because when he does not, a other item is maybe still selected and causes problems.

    Do you mean you want to achieve the pre-selection function like this topic? https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/selection#single-pre-selection

    Wednesday, November 18, 2020 9:34 AM
  • User386366 posted

    I finally found a solution ! When I just set the Property "Loop="True" then the CarouselView is not crashing anymore !!! I am so happy, although it is just a workaround !

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, November 19, 2020 9:00 AM