CreateRendererWithContext .net MAUI android
0
i am re using xamarin forms renderer in MAUI. while creating renderer with "CreateRendererWithContext" method getting exception.
below is my code.
using AndroidPlatform = Microsoft.Maui.Controls.Compatibility.Platform.Android.Platform;
static IVisualElementRenderer GetRenderer(VisualElement element, Context context)
{
var renderer = AndroidPlatform.GetRenderer(element);
if (renderer == null)
{
renderer = AndroidPlatform.CreateRendererWithContext(element, context);
AndroidPlatform.SetRenderer(element, renderer);
}
return renderer;
}
on this line getting exception. renderer = AndroidPlatform.CreateRendererWithContext(element, context);
Exception details :
{Android.Views.InflateException: Binary XML file line #1 in com.companyname.librarytestappmaui:layout/fragment_backstack: Error inflating class androidx.fragment.app.FragmentContainerView ---> Java.Lang.IllegalStateException: FragmentManager is already executing transactions …} Android.Views.InflateException
could someone please help on this.
Developer technologies | .NET | .NET MAUI
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) • 50,156 Reputation points • Microsoft External Staff
2023-06-02T06:00:01.3733333+00:00 After investigation,
Microsoft.Maui.Controls.Compatibility.Platform.Android.Platformhas been deprecated in MAUI.In MAUI, it is recommended that you use the handler to operate the control, you can refer to the following documentations:
If you want to continue using renderer in MAUI, please refer to Using Custom Renderers in .NET MAUI.
-
Anonymous
2023-06-02T15:54:11.4766667+00:00 HI @Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) ,
Thanks for the reply. my project is multi project (migrated xamarin forms project to MAUI). i want to continue using renderer in MAUI . i am using renderers in MAUI with compatibility packages.
i want to attach the project. But i am not getting the option to attached the project.
below is my MauiProgram.cs in Android project.
using System; using Microsoft.Maui.Controls.Compatibility.Hosting; namespace LibraryTestAppMaui.Android { public static class MauiProgram { public static MauiAppBuilder builder; public static MauiAppBuilder Builder { get { if (builder == null) { builder = MauiApp.CreateBuilder(); } return builder; } } public static MauiApp CreateMauiApp() { var builder = Builder; builder .UseMauiCompatibility() .UseMauiApp<App>(); builder.ConfigureMauiHandlers(handlers => { #if ANDROID handlers.AddHandler(typeof(KeyboardWrapperPage), typeof(KeyboardWrapperPageRenderer)); #endif }); builder.Services.AddTransient<MainPage>(); return builder.Build(); } } }below is my App.Xaml.cs
using System; using Microsoft.Maui; using Microsoft.Maui.Controls; using Microsoft.Maui.Controls.Xaml; namespace LibraryTestAppMaui { public partial class App : Application { public static new App Current => (App)Application.Current; public App () { InitializeComponent(); // MainPage = mainPage; MainPage = CreateMainPage(new NavigationPage()); } private Page CreateMainPage(NavigationPage navigationPage) { return new KeyboardWrapperPage(navigationPage); } protected override void OnStart () { } protected override void OnSleep () { } protected override void OnResume () { } } }below is KeyboardWrapperPage
using System; namespace LibraryTestAppMaui { public class KeyboardWrapperPage : Page, IPageContainer<Page> { public KeyboardWrapperPage(Page innerPage) { BackgroundColor = Colors.White; CurrentPage = innerPage; InternalChildren.Add(innerPage); } public Page CurrentPage { get; } protected override bool OnBackButtonPressed() { return CurrentPage.SendBackButtonPressed(); } } }below is my KeyboardWrapperPageRenderer in Android.
using System; using Android.Content; using Android.Views; using Android.Widget; using Microsoft.Maui.Controls.Compatibility.Platform.Android; using Microsoft.Maui.Controls.Platform; using Microsoft.Maui.Platform; using AndroidPlatform = Microsoft.Maui.Controls.Compatibility.Platform.Android.Platform; namespace LibraryTestAppMaui.Android { public class KeyboardWrapperPageRenderer : Microsoft.Maui.Controls.Handlers.Compatibility.VisualElementRenderer<KeyboardWrapperPage> { bool disposed; FrameLayout pageContainer; IPageController PageController => Element; int KEYBOARD_HEIGHT => (int)Context.Resources.GetDimension(260); public KeyboardWrapperPageRenderer(Context context) : base(context) { AutoPackage = false; } protected override void OnElementChanged(ElementChangedEventArgs<KeyboardWrapperPage> e) { base.OnElementChanged(e); if (e.NewElement != null) { var bottomBarPage = e.NewElement; if (pageContainer == null) { pageContainer = CreatePageContainer(Context); AddView(pageContainer); RequestLayout(); } UpdateContent(); } } protected override void OnAttachedToWindow() { base.OnAttachedToWindow(); PageController.SendAppearing(); } protected override void OnDetachedFromWindow() { base.OnDetachedFromWindow(); PageController.SendDisappearing(); } void UpdateContent() { pageContainer.RemoveAllViews(); var page = Element?.CurrentPage; if (page != null) { pageContainer.AddView(GetRenderer(page, Context).View); // AddView(Keyboard.KeyboardView); } } protected override void Dispose(bool disposing) { if (disposing && !disposed) { disposed = true; RemoveAllViews(); var pageRenderer = AndroidPlatform.GetRenderer(Element.CurrentPage); if (pageRenderer != null) { pageRenderer.View.RemoveFromParent(); pageRenderer.Dispose(); } pageContainer?.Dispose(); } base.Dispose(disposing); } static FrameLayout CreatePageContainer(Context context) { return new FrameLayout(context) { LayoutParameters = new FrameLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent, GravityFlags.Fill) }; } static IVisualElementRenderer GetRenderer(VisualElement element, Context context) { var renderer = AndroidPlatform.GetRenderer(element); if (renderer == null) { renderer = AndroidPlatform.CreateRendererWithContext(element, context); AndroidPlatform.SetRenderer(element, renderer); } return renderer; } } }i am using KeyboardWrapperPage as MainPage in App.Xaml.cs.
Please help me on this.
-
Anonymous
2023-06-02T16:02:47.5233333+00:00 HI @Yonglun Liu (Shanghai Wicresoft Co,.Ltd.)
Thanks for the reply. i want to continue using renderer in MAUI. mine is multi project (migtared Xamarin forms to MAUI). i am re using renderers with MAUI compatibility packages.
i want to attach the project. i am not getting the option to attach. i am using KeyboardWrapperPage as MainPage in App.Xaml.cs
Plesae help me on this.
below is my KeyboardWrapperPage class in forms project
using System; namespace LibraryTestAppMaui { public class KeyboardWrapperPage : Page, IPageContainer<Page> { public KeyboardWrapperPage(Page innerPage) { BackgroundColor = Colors.White; CurrentPage = innerPage; InternalChildren.Add(innerPage); } public Page CurrentPage { get; } protected override bool OnBackButtonPressed() { return CurrentPage.SendBackButtonPressed(); } } }below is my KeyboardWrapperPageRenderer class in android project
using System; using Android.Content; using Android.Views; using Android.Widget; using Microsoft.Maui.Controls.Compatibility.Platform.Android; using Microsoft.Maui.Controls.Platform; using Microsoft.Maui.Platform; using AndroidPlatform = Microsoft.Maui.Controls.Compatibility.Platform.Android.Platform; namespace LibraryTestAppMaui.Android { public class KeyboardWrapperPageRenderer : Microsoft.Maui.Controls.Handlers.Compatibility.VisualElementRenderer<KeyboardWrapperPage> { bool disposed; FrameLayout pageContainer; IPageController PageController => Element; int KEYBOARD_HEIGHT => (int)Context.Resources.GetDimension(260); public KeyboardWrapperPageRenderer(Context context) : base(context) { AutoPackage = false; } protected override void OnElementChanged(ElementChangedEventArgs<KeyboardWrapperPage> e) { base.OnElementChanged(e); if (e.NewElement != null) { var bottomBarPage = e.NewElement; if (pageContainer == null) { pageContainer = CreatePageContainer(Context); AddView(pageContainer); RequestLayout(); } UpdateContent(); } } protected override void OnAttachedToWindow() { base.OnAttachedToWindow(); PageController.SendAppearing(); } protected override void OnDetachedFromWindow() { base.OnDetachedFromWindow(); PageController.SendDisappearing(); } void UpdateContent() { pageContainer.RemoveAllViews(); var page = Element?.CurrentPage; if (page != null) { pageContainer.AddView(GetRenderer(page, Context).View); // AddView(Keyboard.KeyboardView); } } protected override void Dispose(bool disposing) { if (disposing && !disposed) { disposed = true; RemoveAllViews(); var pageRenderer = AndroidPlatform.GetRenderer(Element.CurrentPage); if (pageRenderer != null) { pageRenderer.View.RemoveFromParent(); pageRenderer.Dispose(); } pageContainer?.Dispose(); } base.Dispose(disposing); } static FrameLayout CreatePageContainer(Context context) { return new FrameLayout(context) { LayoutParameters = new FrameLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent, GravityFlags.Fill) }; } static IVisualElementRenderer GetRenderer(VisualElement element, Context context) { var renderer = AndroidPlatform.GetRenderer(element); if (renderer == null) { renderer = AndroidPlatform.CreateRendererWithContext(element, context); AndroidPlatform.SetRenderer(element, renderer); } return renderer; } } }below is my MauiProgram.cs in android project.
using System; using Microsoft.Maui.Controls.Compatibility.Hosting; namespace LibraryTestAppMaui.Android { public static class MauiProgram { public static MauiAppBuilder builder; public static MauiAppBuilder Builder { get { if (builder == null) { builder = MauiApp.CreateBuilder(); } return builder; } } public static MauiApp CreateMauiApp() { var builder = Builder; builder .UseMauiCompatibility() .UseMauiApp<App>(); builder.ConfigureMauiHandlers(handlers => { #if ANDROID handlers.AddHandler(typeof(KeyboardWrapperPage), typeof(KeyboardWrapperPageRenderer)); #endif }); builder.Services.AddTransient<MainPage>(); return builder.Build(); } } }below is my App.Xaml.cs
using System; using Microsoft.Maui; using Microsoft.Maui.Controls; using Microsoft.Maui.Controls.Xaml; namespace LibraryTestAppMaui { public partial class App : Application { public static new App Current => (App)Application.Current; public App () { InitializeComponent(); // MainPage = mainPage; MainPage = CreateMainPage(new NavigationPage()); } private Page CreateMainPage(NavigationPage navigationPage) { return new KeyboardWrapperPage(navigationPage); } protected override void OnStart () { } protected override void OnSleep () { } protected override void OnResume () { } } } -
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) • 50,156 Reputation points • Microsoft External Staff
2023-06-05T03:17:30.3166667+00:00 Thanks for your feedback.
According to Mark renderers and related base classes as obsolete #5322 in GitHub, you should use
Microsoft.Maui.Controls.Handlers.Compatibility.Visual ElementRendererinstead. -
Anonymous
2023-06-05T07:16:22.3133333+00:00 @Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) i am using same (Microsoft.Maui.Controls.Handlers.Compatibility.Visual ElementRenderer) in KeyboardWrapperPageRenderer.cs
still getting exception.
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) • 50,156 Reputation points • Microsoft External Staff
2023-06-06T05:28:45.7533333+00:00 Thanks for your feedback.
After investigation, if the parameter passed when building
KeyboardWrapperPageisContentPage,CreateRendererWithContextworks as expected, and this error will occur if it is aNavigationPage.For this issue, it is recommended that you submit feedback to the GitHub repository to make our product group aware of it.
-
Anonymous
2023-06-07T16:14:01.53+00:00 Hi @Yonglun Liu (Shanghai Wicresoft Co,.Ltd.)
Thanks for your reply. In my case i cannot pass ContentPage. I have to pass NavigationPage only.
it is working in xamarin forms but not working in MAUI.
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) • 50,156 Reputation points • Microsoft External Staff
2023-06-08T09:21:55.52+00:00 it is working in xamarin forms but not working in MAUI.
Since the same code works differently in Xamarin and Maui.
It is more recommended that you publish this issue to Maui issues to make our product group aware of it.
-
Anonymous
2023-06-09T09:10:37.17+00:00 @Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) as suggested by you i have raised issue in https://github.com/dotnet/maui/issues. waitimg for their reply.
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) • 50,156 Reputation points • Microsoft External Staff
2023-06-12T02:00:13.5666667+00:00 Thanks for your feedback.
Sign in to comment