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

PhotoGallery fails when updated to current NuGets #74

Open
WillAutio opened this issue Sep 13, 2021 · 1 comment
Open

PhotoGallery fails when updated to current NuGets #74

WillAutio opened this issue Sep 13, 2021 · 1 comment

Comments

@WillAutio
Copy link

First off, thanks for the PhotoGallery part of the app. I learned lots from it.
Background:
Something that did not click until I came across it on a blog:
http://lpains.net/articles/2020/xamarin-forms-collection-view-selected-items/
was that when the docs say to use this:
public ObservableCollection<object> SelectedPhotos
they actually mean object not what ever object you are using in the CollectionView. As long as I was using myObject things were failing a bit. It was almost working, but not quite. When I changed myObject to object. Things started looking up.

Issue:
Now, that I was using object in the SelectedPhotos, things like the LongPress would select the photo that was receiving the LongPress. However, any Photo that I tapped after that got ignored.

Well, what actually happened is that in this code in PhotoGalleryViewModel:

private void OnPressed(Photo obj)
{
        if (_selectionMode != SelectionMode.None)
        { 
            Debug.WriteLine($"Added {obj.ImageSrc}");
            if (_selectedPhotos.Contains(obj))
                SelectedPhotos.Remove(obj);
            else
                SelectedPhotos.Add(obj);
        }
        else
        {
            Shell.Current.GoToAsync($"photo?src={obj.ImageSrc}");
        }
    }

when I was doing the second and subsequent tap of a multi-select, the program would enter OnPressed and enter the first if block since SelectionMode was Multiple. Then it wrote out the Debug stuff. Then it checked if the obj was already in _selectedPhotos. Lo and hehold, for every tap but the first LongPress, obj was contained in _selectedPhotos - and so it got removed. (this code I expect was intended to remove an entry from the list upon the Photo being tapped a second time)

After comparing your code and my code and many other things, I found the cause of the issue. My NuGets were newer than yours. I'll list what I am using:
Xamarin Community Toolkit 1.3.0-pre2
Xamarin.Essentials 1.7.0
Xamarin.Forms 5.0.0.2083
Visual Studio 2019 current
Win 10

When I updated this app to the same versions, it behaved the same way.

It looks like something in what got updated now takes care of the adding to the SelectedPhotos as well as removing ones that get taped a second time. When I remove this code:

              if (_selectedPhotos.Contains(obj))
                    SelectedPhotos.Remove(obj);
                else
                    SelectedPhotos.Add(obj);

it all seems to work - at least for what I tested.

@toumir
Copy link

toumir commented Apr 17, 2022

.

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

2 participants