Bug 60533 - Using UIMenuItem from an Objective-C binding project, action selector will not work on Xamarin.Forms
Summary: Using UIMenuItem from an Objective-C binding project, action selector will no...
Status: RESOLVED INVALID
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 11.0 (xcode9)
Hardware: PC Windows
: --- normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-11-06 17:28 UTC by Shirley Gong
Modified: 2017-11-07 23:42 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
This is the build log generated from VS Mac (69.94 KB, text/plain)
2017-11-06 20:29 UTC, Shirley Gong
Details
This is version info. (2.14 KB, text/plain)
2017-11-06 20:29 UTC, Shirley Gong
Details
This is my project. (123 bytes, text/plain)
2017-11-06 20:40 UTC, Shirley Gong
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description Shirley Gong 2017-11-06 17:28:52 UTC
I am using an Objective-C library that has a UIMenuController with a number of UIMenuItem.

The menu items are initialized as

    UIMenuItem* menuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedStringFromTableInBundle(@"Text", @"Tools", stringBundle, @"Text tool name") action:@selector(createText)];

where 

    -(void)createText
    {
        NSLog(@"Tool::createText");
    }

The Objective-C UIView is added as a subview of a Xamarin UIView. The menu is added on the Objective-C UIView. So the menu should show up without any extra code. 

On Xamarin.iOS, running the binding project, the menu works perfectly fine.

However, on Xamarin.Forms, with PageRenderer, the same menu no longer handle click event. createText function is not called at all.

Please help! Thanks.
Comment 1 Alex Soto [MSFT] 2017-11-06 19:04:33 UTC
Hello, we will need a test case to reproduce and provide a solution to your issue.

Also please include your full build logs, crash reports (if any) and all your version information.

To get full build logs just set the log verbosity to diagnostic at the following locations:
- On Visual Studio for Mac: Preferences > Projects > Build
- On Visual Studio for Windows: Tools > Options > Projects and Solutions > Build and Run

On Visual Studio Windows you also want to add `-v -v -v -v` to the mtouch additional arguments by right-clicking the project in the solution explorer and selecting `Properties`.
Note: this is done automatically on Visual Studio for Mac when the log verbosity is set to diagnostic.

The easiest way to get exact version information:
- On Visual Studio for Mac: "Visual Studio" menu, "About Visual Studio" item, "Show Details" button.
- On Visual Studio for Windows: "Help menu", "About Microsoft Visual Studio" item.
Then copy/paste the version information (you can use the "Copy Information" button).
Comment 2 Shirley Gong 2017-11-06 20:29:25 UTC
Created attachment 25569 [details]
This is the build log generated from VS Mac
Comment 3 Shirley Gong 2017-11-06 20:29:42 UTC
Created attachment 25570 [details]
This is version info.
Comment 4 Shirley Gong 2017-11-06 20:40:29 UTC
Created attachment 25571 [details]
This is my project.

The attachment is large, I've uploaded them here:
http://pdftron.s3.amazonaws.com/custom/external/xamarin/bug_20171106.zip


Step to reproduce:

1. In `PDFNetXamarinOpenSource.iOS-6.7.1.20170927` folder, open `PDFNetXamarinOpenSource.iOS-6.7.1.20170927\projectSrc\PDFNetiOSXamarinPDFNetiOSXamarin.sln`
2. Compile and run sample project `PDFNetiOSXamarinSample`
3. Long press anywhere will show the menu of interest in this ticket.
3. Click any of the option, for example `Note`, will show some action.
4. The above shows that the menu item works in Xamarin.iOS

5. Now, open `FormsDemo.sln` in `FormsDemo` folder
6. Update the reference import to point to `PDFNetXamarinOpenSource.iOS-6.7.1.20170927\lib\ios` (PDFNetiOS.dll and Tools.dll)
7. compile and run the `FormsDemo` project
8. Long press anywhere will show the menu of interest.
9. Click any of the options, nothing happens.
Comment 5 Alex Soto [MSFT] 2017-11-07 04:42:55 UTC
Hello Shirley, 

I do not think there is an issue with Xamarin.iOS at all, I am able to strikeout, squiggle, highlight just fine using the Xamarin.Forms sample.

From the description you give in your initial comment

> The menu items are initialized as
> 
>     UIMenuItem* menuItem = [[UIMenuItem alloc] InitWithTitle:NSLocalizedStringFromTableInBundle(@"Text", @"Tools", stringBundle, @"Text tool name") action:@selector(createText)];
> 
> where 
> 
>     -(void)createText
>     {
>         NSLog(@"Tool::createText");
>     }

I see that this is not happening on the managed side but in the native side of the house, since I can definitely highlight or strikeout text and also you have a long-press handler on both native and managed that works see the following output:

>Hello From Managed::HandlePdfScrollViewLongPress
>PanTool::handleLongPress
>PanTool::attachInitialMenuItems
>PanTool::attachInitialMenuItems::createFreeText

Leads me to believe that the issue is on the native side (PDFTron) that is not finding the selector that is set in the `action:` argument of the UIMenuItem. I would suggest creating a managed representation of the selector and decorate it with [Export] attribute using the same selector from the `action:` target inside the renderer, For example

>[Export ("createText")]
>void CreateText { /*do something here*/ }

and see if it gets hit. 

The thing that could be happening is that your PDFView expects a certain parent controller to be set, if you just add a view from a different controller (SomePDFController.View) into a different ViewController like a PageRenderer derived class, it will not have the selector that the view expects to exist in the parent ViewController.

To continue giving you support we would encourage you create two very minimal samples, one demonstrating the issue and the other one working because there is a lot going on on both samples you attached and makes a little hard to actually spot the code that is failing. We would require the above minimal samples since there is no real issue with Xamarin.iOS product.
Comment 6 Shirley Gong 2017-11-07 23:38:23 UTC
Hi Alex,

Thanks a lot for all your kind help!

In native code, I changed long press menu showing condition by adding:

```
if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
// then show menu
```

This seem to have solved the problem. :)

It seems like anything other than UIGestureRecognizerStateEnded would cause the problem.

Just want to follow up in case anything encounters similar things in the future.
Comment 7 Alex Soto [MSFT] 2017-11-07 23:42:23 UTC
Thank you Shirley!