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

[Enhancement] [SwipeView] Add example of using a SwipeView in a ListView and CollectionView #8990

Closed
czuck opened this issue Dec 20, 2019 · 10 comments
Labels

Comments

@czuck
Copy link

@czuck czuck commented Dec 20, 2019

Summary

I have an existing ListView I would like to add a SwipeView to. I have tried a number of ways to do this and have access to the ListViewItem in the SwipeItem Invoked function, but have been unsuccessful. An example would be extremely helpful.
In addition, a CollectionView example would be helpful.

@Mikilll94

This comment has been minimized.

Copy link

@Mikilll94 Mikilll94 commented Dec 20, 2019

@czuck
I don't know if this will help you but in #8983 I have uploaded the project in which I am using CollectionView with SwipeView.

@jfversluis

This comment has been minimized.

Copy link
Member

@jfversluis jfversluis commented Dec 23, 2019

Thanks for the questions @czuck.

A quite extensive sample with both SwipeView and CollectionView by @jsuarezruiz can be found here: https://github.com/jsuarezruiz/FavFighters

I'm not sure if we have an example with ListView, but it should work pretty similar. Of course, we'd love to help you more but I think these kinds of questions will work better on the Forums or StackOverflow. Because of that I will be closing this one. I hope the linked sample will already give you some idea on how to proceed.

Thanks!

@jfversluis jfversluis closed this Dec 23, 2019
Triage automation moved this from New to Closed Dec 23, 2019
@czuck

This comment has been minimized.

Copy link
Author

@czuck czuck commented Dec 23, 2019

@Mikilll94 Thanks, but your project doesn't actually implement any invoked swipe events, it only deals with the UI.
@jfversluis While that sample does have an extensive UI, again there is no code behind. Tapping on the Favorites SwipeItemView does nothing.

@jfversluis

This comment has been minimized.

Copy link
Member

@jfversluis jfversluis commented Dec 23, 2019

@czuck I turned your question into a blog post which described how to implement it with both an event and a command. It's based on the sample I referred you to earlier. You can find it here: https://blog.verslu.is/xamarin/xamarin-forms-xamarin/swipeview-interaction-with-xamarin-forms/

If it's still not clear, please let me know what I can improve

@czuck

This comment has been minimized.

Copy link
Author

@czuck czuck commented Dec 23, 2019

@jfversluis Thanks for the blog, it is very helpful!! In the first part of your example, where you use Invoked (not Command) you do not indicate which fighter was swiped. Can you add that to the blog post? I would expect it to be in the EventArgs.SelectedItem but I'm unable to get anything out of EventArgs.

@jfversluis

This comment has been minimized.

Copy link
Member

@jfversluis jfversluis commented Dec 24, 2019

@czuck glad to see it helps! I've updated the post and code for your scenario.

The reason that it's not in the EventArgs (I guess) is that SwipeView is not exclusive to CollectionView so SelectedItem would not always be relevant. As you can see in the other examples, i.e. here: https://blog.verslu.is/xamarin/xamarin-forms-xamarin/swipeview-swipe-all-the-things-in-xamarin-forms-4-4/ you can also add the SwipeView to a simple Grid. SelectedItem would then feel a bit odd.

Is there any particular reason you like to use events over Commands?

@czuck

This comment has been minimized.

Copy link
Author

@czuck czuck commented Dec 24, 2019

@jfversluis I am unable to get Commands to work, and unable to access the CommandParameter when the code executes using either events or commands. In my current implementation, I use the PullAndSwipeGestureRecognizer like this:

<c:PullAndSwipeGestureRecognizer PanUpdated="OnPullAndSwipe"
     CommandParameter="{Binding .}" />

and in that event I can access the Parameter in the PullAndSwipeEventArgs:

                    var item = (MyDetailItem)e.Parameter;

but using this same pattern with SwipeView does not work, at runtime the EventArgs are empty and there doesn't appear to be a SwipeItemEventArgs object.

<SwipeItems Mode="Execute"
  SwipeBehaviorOnInvoked="Close" >
  <SwipeItemView Invoked="OnLeftSwipeInvoked"
  CommandParameter="{Binding .}" >

image

Then I attempted to use Command using the SwipeViewCustomItemPage as my example, but I could not get the command to execute at all. I believe your DataBinding troubles paragraph may explain why, but this code is not MVVM.

@Mikilll94

This comment has been minimized.

Copy link

@Mikilll94 Mikilll94 commented Dec 24, 2019

@czuck
CommandParameter works only with Command, not with simple events, like Invoked.

Your post contain not enough info to find out why Commands are not working in your code. Please upload the whole project.

BTW, I guess that Commands are not working in your project because you need to access the right BindingContext when you are setting Command on a single CollectionView element.

@czuck

This comment has been minimized.

Copy link
Author

@czuck czuck commented Dec 24, 2019

@Mikilll94 Thanks for the offer, but I don't have my issue recreated in a project that could be easily uploaded. I was just looking for direction.
@jfversluis I just discovered if I send the CommandParameter into the event I can get it out of the sender object rather than the event args ..

<SwipeItemView Invoked="OnLeftSwipeInvoked"
  CommandParameter="{Binding .}" >

void OnLeftSwipeInvoked(object sender, EventArgs e)
{
  var item = (MyDetailItem)((SwipeItemView)sender).CommandParameter;
@jfversluis

This comment has been minimized.

Copy link
Member

@jfversluis jfversluis commented Jan 6, 2020

@czuck sure, you should be able to access the CommandParameter that way but it kind of defeats the purpose of the whole thing. Commands are there to separate the UI from the logic so you don't need code in the code-behind of your page like with events.

Like @Mikilll94 mentioned, it might be helpful if you could create a little repro project which shows your issue when trying to use commands so we can have a look and either point out what you could do to fix it or create a bug if something is not working properly ;)

@samhouts samhouts removed this from Closed in Triage Jan 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.