Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a native UWP view from Xamarin Forms view?

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!

like image 527
Mirel Vlad Avatar asked Jun 13 '16 12:06

Mirel Vlad


People also ask

Does Xamarin support UWP?

Bookmark this question. Show activity on this post. When I started my solution in VS Community 2019 it said that the Xamarin Forms Shell did not support UWP. I just upgraded VS to 16.9.

How do I create an Xamarin native project in Visual Studio?

Create a new Xamarin. Start Visual Studio. Click File > New > Project to create a new project. In the new project dialog, select the Mobile App (Xamarin. Forms) template and click Next.


1 Answers

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;
}
like image 155
Mirel Vlad Avatar answered Oct 13 '22 15:10

Mirel Vlad