Xamarin.forms: [Bug] [Android] Cannot add/remove menuitems at runtime to contextActions of a ViewCell

Created on 29 Oct 2019  路  8Comments  路  Source: xamarin/Xamarin.Forms

Description

It is not possible to add or remove menuitems to a viewcell at runtime. This actually only does not work for Android.

Steps to Reproduce

  1. Clone this repository: https://github.com/BrayanKhosravian/AddMenuItemAtRuntime
  2. Select Android as startup project
  3. long press on one of the ViewCell which are visible inside the ListView (just to verify that there is only 1 MenuItem)
  4. Click the "Add"- Button to add a MenuItem at runtime
  5. Long click the same ViewCell
  6. result: no MenuItem was added
  7. try the same with remove

  8. to verify the behavior check the same steps for uwp and ios (result: MenuItems get added)

Expected Behavior

It should be able to add/remove MenuItem麓s at runtime to a ViewCell. This works for Ios and UWP.

Actual Behavior

It is not possible to add/remove MenuItem麓s at runtime to a ViewCell. This works for Ios and UWP.

Basic Information

  • Version with issue: v4.3.0908675 (lower versions as well)
  • Last known good version: -
  • IDE: Visual Studio 2019 v16.3.2
  • Platform Target Frameworks:

    • Android: 9.0

  • Android Support Library Version:
  • Nuget Packages: no external nugets
  • Affected Devices: tested with Huawei p20, but this is a generic issue

Reproduction Link


https://github.com/BrayanKhosravian/AddMenuItemAtRuntime

Screenshots

Not working on android
droid_menuitem

Working on IOS

ios_menuitem

Working on UWP
uwp_menuitem

listview 4 help wanted inactive Android bug up-for-grabs

All 8 comments

@bruzkovsky

Team, I tried to reproduce it with 4.3.0 branch but seems to be working fine on Android (added a new Custom menu item at runtime).

8284_workingfine

Please note: I haven't reproduced exactly the attached reproduction but similar to that. Here's the code to add a menu item in ContextActions at runtime:

var button = new Button()
{
    Text = "Add",
    WidthRequest = 50
};

var addmenu = new MenuItem { Text = "Custom" };
button.Command = new Command(() => ContextActions.Add(addmenu));

var stack = new StackLayout
{
    Children =
    {
        label, button
    },
    Orientation = StackOrientation.Vertical
};
View = stack;

@techduggu thats strange actually, because i verified the issue for this versions: v4.1.0, v4.3.0908675 and for version 4.4.0.

I forked the xamarin forms repository and branched away from the master (after v4.3.0 got merged into v4.4.0) and created an issue in the control gallery.
Could you either share a repository or tell what u did differently compared to my reproduction?

I could reproduce this issue 3 times with different version. Im very curious.

heres my source code for the issue i created in the control gallery:
Issue8284.xaml: https://hastebin.com/xaciyebasu.xml
Issue8284.xaml.cs: https://hastebin.com/eranevexob.cs

@BrayanKhosravian I pointed to 4.3.0 branch and used existing Issue3652.cs (as it already contains Custom ViewCell). You can just replace the ICell class with the below code (as I shared earlier) and try running it at your end. Working for me but let me know how it goes.
Further information: I've tested on Android Emulator (API 27).

public class ICell : ViewCell
{
    public ICell()
    {
        var label = new Label();
        label.SetBinding(Label.TextProperty, "Description");
        label.AutomationId = "pandabear";
        var menu = new MenuItem { Text = "Remove" };
        menu.Command = new Command(() => ((ListItemViewModel)BindingContext).Remove.Execute((this, BindingContext)));
        ContextActions.Add(menu);
        var button = new Button()
        {
            Text = "Add",
            WidthRequest = 50
        };

        var addmenu = new MenuItem { Text = "Custom" };
        button.Command = new Command(() => ContextActions.Add(addmenu));

        var stack = new StackLayout
        {
            Children =
            {
                label, button
            },
            Orientation = StackOrientation.Vertical
        };
        View = stack;
    }
}

@techduggu ok i tested it and it seems to work.
Using a real phone (Huawei p20) with android version 9.

Ok as this might work for issue 3652 but not for my new issue, there might be something wrong.
im debugging through the source code right now, lets see what i can find.

But the main difference which i can see is, that my repro-issue uses a view in xaml and a primitive datatype for the ObservableCollection

Issue3652 uses only codebehind and a seperate class for the ObservableCollection.
Did u try to clone my repository and reproduce that it does not work?

@techduggu ok i finally found the cause of the bug 馃槃馃檶

  • The listview in the issue "Issue3652.cs" does not use a CacheingStrategy.
  • Replace the ICell posted from techduggo which was mentioned earlier
  • Open the file "Issue3652.cs" and set CacheingStrategy to ListViewCachingStrategy.RecycleElement, then retry again. It fails.
  • i think that the correct menuitems should be shown even when recylcing elements

  • ill debug a bit deeper into.

Image:
image

The original repro definitely demonstrates the issue. And if you remove CachingStrategy="RecycleElement", the context menus update as expected.

Was this page helpful?
0 / 5 - 0 ratings