I'm working on a UWP version of NControl for use in my Xamarin.Forms app. NControl is a simple Xamarin.Forms wrapper control around the NGraphics library that allows you to draw stuff with code. Pretty slick: https://github.com/chrfalch/NControl
For each platform, NControl defines a customer renderer. This renderer works just fine, except when I turn on the option to Compile with .NET Native tool chain in the UWP project build properties - then it simply doesn't show up on screen at all. Through debugging, I've confirmed that with this option enabled, the custom renderer's constructor never gets called, and the object isn't in the app's live visual tree either.
Any idea why this would be?
I found out that there is a special overload of the Forms.Init method just for UWP in which you can pass in an IEnumerable called rendererAssemblies. I have to include the Assembly for my custom renderer in that call in order for it to work on UWP using the Compile with .NET Native too chain option.
This is described in the Xamarin docs here, but I didn't think it applied to me since I wasn't getting that exception (or any exceptions)
I found that this is a requirement for ALL custom renderers that live in a 3rd party library. For example, our app has a custom renderer for playing videos. It has an implementation for each platform (iOS & UWP) that lives in its respective platform-specific projects, and this renderer works great. However, in addition to NControl, we're also using the popular Xamarin Image Circle Control plugin and it isn't working correctly in .NET Native builds, leaving the images square. Same issue.
here's the bit of code that corrected it:// For .NET Native compilation, you have to tell Xamarin.Forms which assemblies it should scan for custom controls and renderers var rendererAssemblies = new[] { typeof(NControl.UWP.NControlViewRenderer).GetTypeInfo().Assembly, typeof(ImageCircle.Forms.Plugin.UWP.ImageCircleRenderer).GetTypeInfo().Assembly }; Xamarin.Forms.Forms.Init(e, rendererAssemblies);
Even though I was calling the Init methods on these libraries, without the above code, you get nothing in UWP with .NET Native builds. No exceptions are thrown. Nothing in the debug output window. Nothing on screen. Silent failure.ImageCircleRenderer.Init(); NControl.UWP.NControlViewRenderer.Init();
I feel that this should be documented better.
Answers
I found out that there is a special overload of the Forms.Init method just for UWP in which you can pass in an IEnumerable called rendererAssemblies. I have to include the Assembly for my custom renderer in that call in order for it to work on UWP using the Compile with .NET Native too chain option.
This is described in the Xamarin docs here, but I didn't think it applied to me since I wasn't getting that exception (or any exceptions)
I found that this is a requirement for ALL custom renderers that live in a 3rd party library. For example, our app has a custom renderer for playing videos. It has an implementation for each platform (iOS & UWP) that lives in its respective platform-specific projects, and this renderer works great. However, in addition to NControl, we're also using the popular Xamarin Image Circle Control plugin and it isn't working correctly in .NET Native builds, leaving the images square. Same issue.
here's the bit of code that corrected it:
// For .NET Native compilation, you have to tell Xamarin.Forms which assemblies it should scan for custom controls and renderers var rendererAssemblies = new[] { typeof(NControl.UWP.NControlViewRenderer).GetTypeInfo().Assembly, typeof(ImageCircle.Forms.Plugin.UWP.ImageCircleRenderer).GetTypeInfo().Assembly }; Xamarin.Forms.Forms.Init(e, rendererAssemblies);
Even though I was calling the Init methods on these libraries, without the above code, you get nothing in UWP with .NET Native builds. No exceptions are thrown. Nothing in the debug output window. Nothing on screen. Silent failure.
ImageCircleRenderer.Init(); NControl.UWP.NControlViewRenderer.Init();
I feel that this should be documented better.