I'm migrating from Xamarin to MAUI and i have a native Android layout that i use to take photos. In Xamarin i use a PageRenderer and on the OnElementChanged event i assign the view:
view = activity.LayoutInflater.Inflate(Resource.Layout.CameraLayout, this, false);
AddView(view);
In MAUI Android i've attempted to do what i believe is the same. I inherit from PageHandler:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorBlack">
</FrameLayout>
handlers.AddHandler(typeof(Views.CameraPage), typeof(Platforms.Android.Renderers.CameraPageRenderer));
protected override void ConnectHandler(ContentViewGroup platformView)
{
base.ConnectHandler(platformView);
var view = activity.LayoutInflater.Inflate(Resource.Layout.Test, platformView, false);
platformView.AddView(view);
}
I can see the code being executed without any errors but the screen is always blank. I've no issues in iOS however with this code:
[code to create view]
MainView = VirtualView.ToPlatform(MauiContext);
MainView.Add(newView);
I can do similar in Android but you cant add a subview to the MainView like iOS. So this would work for changing the background colour:
var MainView = VirtualView.ToPlatform(MauiContext);
MainView.SetBackgroundColor(Droid.Graphics.Color.Red);
I've tried manually coding a simple View in Android rather than reading from resource axml but with the same issue. I can see examples for content view / control renderers on Microsoft web resources but can't find an example for PageRenderer. Help appreciated.
Well, as an answer:
For PageHandler
, Debug it and it does run this code
var view = activity.LayoutInflater.Inflate(Resource.Layout.Test, platformView, false);
platformView.AddView(view);
But as you said that "the screen is always blank". We also tried other ways but they all ended in failure. You can report it on MAUI issue GitHub. Let the developers know and deal with it.
For PageRenderer
, although it belongs to xamarin, it can still be used via UseMauiCompatibility()
| AddCompatibilityRenderer()
. And you refer to this: Using Custom Renderers in .NET MAUI.
Hope these can help you.