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
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows (show other bugs)
Version: 2.3.4
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Stephane Delcroix
URL:
Depends on:
Blocks:
 
Reported: 2017-04-26 16:44 UTC by Taylor Buchanan
Modified: 2017-06-16 22:23 UTC (History)
2 users (show)

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


Attachments
Reproduction (125.66 KB, application/x-zip-compressed)
2017-04-26 16:44 UTC, Taylor Buchanan
Details
Screenshot of OnPlatform.s_valueConverter being null (83.96 KB, image/png)
2017-04-26 16:46 UTC, Taylor Buchanan
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 FIXED

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

#### 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 2.3.4.231 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

typeof(DependencyService).GetTypeInfo().Assembly.GetType(@"Xamarin.Forms.Xaml.ValueConverterProvider")

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
https://github.com/xamarin/Xamarin.Forms/pull/890
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!