Bug 55636

Summary: OnPlatform doesn't work on UWP when compiling with .NET Native
Product: Forms Reporter: Taylor Buchanan <taylor.buchanan>
Component: WindowsAssignee: Stephane Delcroix <stephane.delcroix>
Severity: normal CC: jas, jimmy.garrido
Priority: ---    
Version: 2.3.4   
Target Milestone: ---   
Hardware: PC   
OS: Windows   
Tags: uwp .net native onplatform Is this bug a regression?: ---
Last known good build:
Attachments: Reproduction
Screenshot of OnPlatform.s_valueConverter being null

Description Taylor Buchanan 2017-04-26 16:44:39 UTC
Created attachment 21809 [details]

#### Overview:
When compiling a UWP app with "Compile with .NET Native tool chain" enabled, OnPlatform doesn't work at all.

#### Steps to Reproduce:
1. Use the OnPlatform class in C# or XAML
2. Enable "Compile with .NET Native tool chain" in your build configuration

#### Actual Results:
The default value for the type is always returned during implicit conversion.

#### Expected Results:
The value specified for the "Windows" platform should be returned during implicit conversion.

#### Build Date & Hardware:
Xamarin.Forms on Windows 10 Enterprise 1607

#### Additional Information:
From what I can tell, this has something to do with the DependencyService registration for ValueConverterProvider. When I debug my company's app with .NET Native enabled, I can see OnPlatform.s_valueConverter is null (see attached screenshot). This is what appears to be causing the issue.

When I try to use reflection to retrieve the ValueConverterProvider type as shown below, I get null. This seems to indicate the class is being stripped from the final package. I'm thinking it might have something to do with this: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/introduction/#Universal_Windows_Platform_.NET_Native_Compilation


This is a pretty big problem for apps using OnPlatform heavily.
Comment 1 Taylor Buchanan 2017-04-26 16:46:58 UTC
Created attachment 21810 [details]
Screenshot of OnPlatform.s_valueConverter being null
Comment 2 Taylor Buchanan 2017-04-26 17:08:15 UTC
Disregard my note about using reflection to retrieve the ValueConverterProvider type. I overlooked that ValueConverterProvider was not in the same assembly as IValueConverterProvider even though they are in the same namespace.
Comment 3 Taylor Buchanan 2017-04-26 17:25:30 UTC
I've found a workaround. Looks like the issue is related to the DependencyService registration like I mentioned. Adding the following to my app fixes the issue:

Xamarin.Forms.Forms.Init(e, new [] { typeof(Xamarin.Forms.Xaml.Extensions).GetTypeInfo().Assembly });

This allows DependencyService to scan the Xamarin.Forms.Xaml assembly and include the ValueConverterProvider dependency.

Can you please add this assembly by default or perhaps document this somewhere if it is intentional?
Comment 4 Stephane Delcroix 2017-04-28 06:51:55 UTC
Thanks for the report, and the investigation
Comment 5 Stephane Delcroix 2017-04-28 07:56:19 UTC
Comment 6 Jimmy [MSFT] 2017-04-28 14:57:03 UTC
This issue is occurring when using the new OnPlatform syntax introduced in 2.3.4[1]. It does not happen when using the old syntax.

[1] https://github.com/xamarin/Xamarin.Forms/pull/658
Comment 7 Jimmy [MSFT] 2017-06-16 22:23:50 UTC
The fix referenced in comment 5 has been merged and should be available in 2.3.5-pre3. Thanks!