giuann
giuann

Reputation: 51

Is it possible to use Xamarin Forms Controls in a Uno project?

I am currently developing a cross-platform (Windows and Mac) app using Uno and WinUI 3. I need to implement a WebView in my project, but I'm having some trouble.

On Windows I can use the new WebView2 from WinUI without any problem, but being not supported by Uno I can't use it on Mac. On Mac I think I can't even use the WebView1 because it is not supported anymore in WinUI 3 (and so in Uno.WinUI, or am I wrong?).

So I thought to use the Xamarin.Forms WebView on Mac, but I don't know how to implement it. I thought to add a new project to the solution with just the WebView and call it from the Uno.Mac project, but it doesn't work.

May you give me any suggestions?

Upvotes: 1

Views: 474

Answers (3)

Patrick Eira
Patrick Eira

Reputation: 1

You can easily add Xamarin content into uno and vice versa.

Just try this :

public partial class XamarinContent : ContentControl
    {

        View _element;
        public View Element
        {
            get => _element;
            set
            {
                _element = value;

#if __ANDROID__
                var renderer = RendererFactory.GetRenderer(value);
                this.Content = renderer.View;
                this.SizeChanged += (s, a) => {
                    _element.Layout(new Rect(0, 0, a.NewSize.Width, a.NewSize.Height));
                };
#else
                var converter = new ViewToRendererConverter();
                var frameworkElement = converter.Convert(value, null, null, null);
                this.Content = frameworkElement;
               // this.SetValue(ContentControl.ContentProperty, frameworkElement) ;
#endif
            }
        }
    }

and to convert from uno into Xamarin forms

   [ContentProperty("Element")]
    public class UnoContent : Xamarin.Forms.TemplatedView
    {
        public static readonly BindableProperty ContentProperty
            = BindableProperty.Create("Element", typeof(Windows.UI.Xaml.FrameworkElement), typeof(View), null);

        public Windows.UI.Xaml.FrameworkElement Element
        {
            get { return (Windows.UI.Xaml.FrameworkElement)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); base.ControlTemplate = new Xamarin.Forms.ControlTemplate(() => value.ToView()); }
        }

        
    }

Upvotes: 0

Jérôme Laban
Jérôme Laban

Reputation: 5282

Xamarin.Forms controls cannot be used individually in an Uno app, in the same way those cannot be used in a Xamarin.iOS or Xamarin.Android "classic" app.

You can, however, use the the Xamarin.Forms embedding technique described in this article. The base of the technique is to create a ContentPage, then add it as a native control.

Uno Platform supports adding native controls directly in the Visual Tree by assigning the native view to the Content property of a ContentControl using code-behind.

Upvotes: 1

Martin Zikmund
Martin Zikmund

Reputation: 39092

It is possible to use Uno Platform controls in Xamarin.Forms app (see docs). The other way may also be possible as there was a way to embed a Xamarin.Forms control in a native app, and Uno allows embedding native controls (see docs).

However, a more efficient solution would be to use the built-in WebView control that Uno Platform provides (unless it is missing some functionality you require - in which case please raise an issue on GitHub :-) ).

Alternatively, you could also directly embed the native macOS WKWebView (using the steps linked to above) and work with it directly.

Upvotes: 3

Related Questions