Bug 58204 - Using Native View Embedding fails if using a Fragment for the native view.
Summary: Using Native View Embedding fails if using a Fragment for the native view.
Status: CONFIRMED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android (show other bugs)
Version: 2.3.5
Hardware: PC Windows
: High normal
Target Milestone: ---
Assignee: E.Z. Hart [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2017-07-18 03:27 UTC by Jon Goldberger [MSFT]
Modified: 2017-10-05 18:17 UTC (History)
3 users (show)

See Also:
Tags: ac nativeview fragment android
Is this bug a regression?: ---
Last known good build:


Attachments
Test Project (79.95 KB, application/zip)
2017-07-18 03:27 UTC, Jon Goldberger [MSFT]
Details
Workaround using custom renderer (103.80 KB, application/zip)
2017-08-08 22:20 UTC, Jon Goldberger [MSFT]
Details

Description Jon Goldberger [MSFT] 2017-07-18 03:27:52 UTC
Created attachment 23616 [details]
Test Project

## Description

Reporting customer is using a library that can only provide an Android Fragment to get the map view provided by the library. In trying to get this to work, customer had issues getting the map fragment to size correctly using an embedded native view. In a discussion on the forums [1], a possible solution was provided that worked as long as the page with the embedded Fragment was the first page assigned to the MainPage property. This worked in the provided example that did not use any mapping library and worked in the customer's solution that used the mapping library that provided the Fragment once the  technique was adopted in the customer's solution. However, if instead the page with the embedded Fragment is a) wrapped in a navigation page,  b) pushed onto the navigation stack, or c) the MainPage property is initially set to another page but later MainPage is set to the page with the Fragment then the page load fails with some variation of a Resource not found exception, with varying resource numbers noted like 0x1 or 0x2. The same is true for both the sample solution provided by EZ Hart [2] and in the customer's solution. 

Note this only seems to be a problem when using a Fragment. In the test solution, there is a FragemntThing class which is the fragment that is to be placed in the Forms app, and a FragmentThingViewGroup class that is used to instantiate and wrap the Fragment so it can be passed back to the Forms UI via a Forms ContentView. The Forms ContentView is used as a place holder, then a message is sent to the Android platform which is subscribed to in MainActivity.cs. When the subscriber code is run, the ContentView.Content is populated with the FragmentThingViewGroup. This all works, as noted, as long as the page with the fragment is the first page set as the App.MainPage. Again, if it is assigned to App.MainPage later, or wrapped in a NavigationPage, or pushed onto the NavigationStack (modal or normal) then the ResourceNotFound exception is thrown. 

>07-17 17:09:13.558 W/ResourceType( 2981): No package identifier when getting name for resource number 0x00000001
>[0:] *****Exception: Exception has been thrown by the target of an invocation.
>  at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00017] in <3fd174ff54b146228c505f23cf75ce71>:0 
>  at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0007a] in <3fd174ff54b146228c505f23cf75ce71>:0 
>  at System.Reflection.MonoCMethod.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0 
>  at System.RuntimeType.CreateInstanceImpl (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes, System.Threading.StackCrawlMark& stackMark) [0x00213] in <3fd174ff54b146228c505f23cf75ce71>:0 
>  at System.Activator.CreateInstance (System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x000a1] in <3fd174ff54b146228c505f23cf75ce71>:0 
>  at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0 
>  at Xamarin.Forms.Xaml.CreateValuesVisitor.CreateFromFactory (System.Type nodeType, Xamarin.Forms.Xaml.IElementNode node) [0x00025] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\CreateValuesVisitor.cs:203 
>  at Xamarin.Forms.Xaml.CreateValuesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, Xamarin.Forms.Xaml.INode parentNode) [0x00097] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\CreateValuesVisitor.cs:65 
>  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x000b1] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:175 
>  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00089] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:172 
>  at Xamarin.Forms.Xaml.RootNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00054] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:223 
>  at Xamarin.Forms.Xaml.XamlLoader.Visit (Xamarin.Forms.Xaml.RootNode rootnode, Xamarin.Forms.Xaml.HydratationContext visitorContext) [0x00053] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:122 
>  at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.String xaml) [0x00046] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:76 
>  at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Type callingType) [0x0002f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:56 
>  at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (TXaml view, System.Type callingType) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ViewExtensions.cs:36 
>  at NativeLayoutParams.MainPage.InitializeComponent () [0x00001] in C:\lp\PCLNativeEmbedFrag\NativeLayoutParams\NativeLayoutParams\obj\Debug\NativeLayoutParams.MainPage.xaml.g.cs:26 
>  at NativeLayoutParams.MainPage..ctor () [0x00008] in C:\lp\PCLNativeEmbedFrag\NativeLayoutParams\NativeLayoutParams\MainPage.xaml.cs:12 
>  at NativeLayoutParams.Page1.Button_Clicked (System.Object sender, System.EventArgs e) [0x00002] in C:\lp\PCLNativeEmbedFrag\NativeLayoutParams\NativeLayoutParams\Page1.xaml.cs:24 





[1] https://forums.xamarin.com/discussion/98427/xf-native-embedding-what-is-the-syntax-to-tell-an-android-fragment-to-match-parent#latest
[2] https://github.com/hartez/PCLNativeEmbeddingAdjustment/tree/fragment




## Steps to reproduce

1. Open the attached test project

2. deploy Android app to an Android device or simulator.

Expected result: Page will display two boxes that both say "I'm a Fragment"

Actual result: Exception is thrown as noted above. 


## Environment

Microsoft Visual Studio Enterprise 2017 
Version 15.2 (26430.15) Release
VisualStudio.15.Release/15.2.0+26430.15
Microsoft .NET Framework
Version 4.6.01586

Installed Version: Enterprise

Architecture Diagrams and Analysis Tools   00369-60000-00001-AA405
Microsoft Architecture Diagrams and Analysis Tools

Visual Basic 2017   00369-60000-00001-AA405
Microsoft Visual Basic 2017

Visual C# 2017   00369-60000-00001-AA405
Microsoft Visual C# 2017

Visual C++ 2017   00369-60000-00001-AA405
Microsoft Visual C++ 2017

Visual F# 4.1   00369-60000-00001-AA405
Microsoft Visual F# 4.1

Application Insights Tools for Visual Studio Package   8.6.00404.2
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2017   15.0.30503.0
ASP.NET and Web Tools 2017

ASP.NET Web Frameworks and Tools 2012   4.0.11203.0
For additional information, visit https://www.asp.net/

ASP.NET Web Frameworks and Tools 2017   5.2.50303.0
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   15.0.30209.0
Azure App Service Tools v3.0.0

Azure Data Lake Node   1.0
This package contains the Data Lake integration nodes for Server Explorer.

Azure Data Lake Tools for Visual Studio   2.2.5000.0
Microsoft Azure Data Lake Tools for Visual Studio

Common Azure Tools   1.9
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Fabric.DiagnosticEvents   1.0
Fabric Diagnostic Events

JavaScript Language Service   2.0
JavaScript Language Service

JavaScript Project System   2.0
JavaScript Project System

JavaScript UWP Project System   2.0
JavaScript UWP Project System

KofePackagePackage Extension   1.0
KofePackagePackage Visual Studio Extension Detailed Info

Merq   1.1.17-rc (cba4571)
Command Bus, Event Stream and Async Manager for Visual Studio extensions.

Microsoft Azure Hive Query Language Service   2.2.5000.0
Language service for Hive query

Microsoft Azure Tools   2.9
Microsoft Azure Tools for Microsoft Visual Studio 2017 - v2.9.50131.1

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   Mono.Debugging.VisualStudio
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   4.2.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

SQL Server Data Tools   15.1.61702.140
Microsoft SQL Server Data Tools

ToolWindowHostedEditor   1.0
Hosting json editor into a tool window

TypeScript   2.2.2.0
TypeScript tools for Visual Studio

Visual C++ for Cross Platform Mobile Development (Android)   15.0.26228.00
Visual C++ for Cross Platform Mobile Development (Android)

Visual C++ for Cross Platform Mobile Development (iOS)   15.0.26228.00
Visual C++ for Cross Platform Mobile Development (iOS)

Visual Studio Tools for Universal Windows Apps   15.0.26430.06
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

Xamarin   4.5.0.486 (fec6f88)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android SDK   7.3.1.2 (9dbc4c5)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK   10.10.0.37 (ad35de4)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
Comment 2 Jon Goldberger [MSFT] 2017-08-08 22:20:47 UTC
Created attachment 24085 [details]
Workaround using custom renderer

I was able to workaround this issue by using a custom renderer. New project with workaround is attached.

Note You need to log in before you can comment on or make changes to this bug.