6
votes

I am trying to create a native UWP view from a Xamarin Forms view. Following the example from here, I managed to do it for Android and IOS.

More precisely, on IOS the conversion looks like this:

public static UIView ConvertFormsToNative(Xamarin.Forms.View view, CGRect size)
{
    var renderer = RendererFactory.GetRenderer (view); 
    renderer.NativeView.Frame = size; 
    renderer.NativeView.AutoresizingMask = UIViewAutoresizing.All;
    renderer.NativeView.ContentMode = UIViewContentMode.ScaleToFill; 
    renderer.Element.Layout (size.ToRectangle()); 
    var nativeView = renderer.NativeView; 
    nativeView.SetNeedsLayout ();

    return nativeView;
}

However, I need a similar approach for UWP.

Any help would be appreciated!

2
I answered a similar question but on android, check if helps: stackoverflow.com/questions/37788731/…jzeferino
I need something closer to the android or IOS solution. I have updated my problem description with the IOS example.Mirel Vlad

2 Answers

3
votes

Ok, I've solved this by writing the following code for uwp:

internal static FrameworkElement ConvertFormsToNative(View view, Rectangle size)
{
     var renderer = Platform.CreateRenderer(view);           

     view.Layout(size);

     return renderer.ContainerElement;
}
0
votes

Old question but i would like to add an answer for the latest Xamarin version. I had same problem in 4.7 version and after digging into the Xamarin's source code, i found out that there is an extension converter method called ViewToRendererConverter can be utilized like below. It handles all childrens rendering and their sizes.

   var viewToRendererConverter = new ViewToRendererConverter();
    var frameworkElement = viewToRendererConverter.Convert(view, null, null, null);