How to create a native UWP view from Xamarin Forms view?
Asked Answered
G

2

7

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!

Gehlbach answered 13/6, 2016 at 12:18 Comment(3)
I answered a similar question but on android, check if helps: #37789231Schizophrenia
Follow this link developer.xamarin.com/guides/xamarin-forms/custom-renderer/…Pileous
I need something closer to the android or IOS solution. I have updated my problem description with the IOS example.Gehlbach
G
4

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;
}
Gehlbach answered 14/6, 2016 at 10:38 Comment(0)
D
1

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);
Downgrade answered 19/7, 2020 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.