AndreiMisiukevich/ContextMenu

ForceCloseCommand not working on first row in ListView iOS

shahbaaz90 opened this issue · 7 comments

hi,
the ForceCloseCommand seems to be not creating the close effect on the first row on the ListView on iOS. works fine on all the other rows.

Using Listview with RecycleElement disabled (enabling it is causing none of the listview context menus to dismiss)

noticing something similar on android too

weird
Can you provide a sample?

testingSwipeCellLib.zip

I have attached the sample code as zip file. the issue is now more apparent when i enable the RecycleElement strategy. it makes no rows dismiss at all, and causes a crash in android at some point.

The control otherwise is pretty impressive. Thanks a lot for your help with this.

@shahbaaz90 try to use it with CollectionView

I found a similar issue but for Android I was able to fix it by overwriting the default ForceCloseCommand instance and perform the ScrollToAsync call twice
using something like this:

public class CustomSideContextMenuView : SideContextMenuView
{
        public CustomSideContextMenuView() : base()
        {
            if (Device.RuntimePlatform == Device.Android)
            {
                ForceCloseCommand = new Command(parameter => ForceClose(parameter is bool boolean ? boolean : true));
            }
        }

       public new void ForceClose(bool animated = true)
            => ForceCloseContextMenu(this, animated);

        public new async void ForceCloseContextMenu(CustomSideContextMenuView view, bool animated)
        {
            if (view == null)
            {
                return;
            }

            try
            {
                if (view.ScrollX > 0)
                {
                    await view.ScrollToAsync(0, 0, animated);
                    await Task.Delay(200);
                    await view.ScrollToAsync(0, 0, animated);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
}

Im using the latest current Forms version "Xamarin.Forms" version="4.4.0.991265" and the collectionview

Feel free to make a PR with a fix or just use the workaround provided above

Found why only the last Command worked:
If you bind it to the ViewModel of the Page, then only the last element will close because it gets overridden by each element in the CollectionView/ListView

In order to make it work, the Command would have to have a parameter which is passed to the command.