Bug 55636 - OnPlatform doesn't work on UWP when compiling with .NET Native
Summary: OnPlatform doesn't work on UWP when compiling with .NET Native
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows (show other bugs)
Version: 2.3.4
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Stephane Delcroix
Depends on:
Reported: 2017-04-26 16:44 UTC by Taylor Buchanan
Modified: 2017-06-16 22:23 UTC (History)
2 users (show)

See Also:
Tags: uwp .net native onplatform
Is this bug a regression?: ---
Last known good build:

Reproduction (125.66 KB, application/x-zip-compressed)
2017-04-26 16:44 UTC, Taylor Buchanan
Screenshot of OnPlatform.s_valueConverter being null (83.96 KB, image/png)
2017-04-26 16:46 UTC, Taylor Buchanan

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!

Notice (2018-05-21): bugzilla.xamarin.com will be switching to read-only mode on Thursday, 2018-05-25 22:00 UTC.

Please join us on Visual Studio Developer Community and GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs and copy them to the new locations as needed for follow-up. The See Also field on each Bugzilla bug will be updated with a link to its new location when applicable.

After Bugzilla is read-only, if you have new information to add for a bug that does not yet have a matching issue on Developer Community or GitHub, you can create a follow-up issue in the new location. Copy and paste the title and description from this bug, and then add your new details. You can get a pre-formatted version of the title and description here:

In special cases you might also want the comments:

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.

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