Example #1
0
        internal static IVisualElementRenderer CreateRenderer(VisualElement element, Context context)
        {
            IVisualElementRenderer renderer = null;

            // temporary hack to fix the following issues
            // https://github.com/xamarin/Microsoft.Maui.Controls.Compatibility/issues/13261
            // https://github.com/xamarin/Microsoft.Maui.Controls.Compatibility/issues/12484
            if (element is RadioButton tv && tv.ResolveControlTemplate() != null)
            {
                renderer = new DefaultRenderer(context);
            }

            // This code is duplicated across all platforms currently
            // So if any changes are made here please make sure to apply them to other platform.cs files
            if (renderer == null)
            {
                IViewHandler handler = null;

                //TODO: Handle this with AppBuilderHost
                try
                {
                    handler = Forms.MauiContext.Handlers.GetHandler(element.GetType());
                    handler.SetMauiContext(Forms.MauiContext);
                }
                catch
                {
                    // TODO define better catch response or define if this is needed?
                }

                if (handler == null)
                {
                    renderer = Registrar.Registered.GetHandlerForObject <IVisualElementRenderer>(element, context)
                               ?? new DefaultRenderer(context);
                }
                // This means the only thing registered is the RendererToHandlerShim
                // Which is only used when you are running a .NET MAUI app
                // This indicates that the user hasn't registered a specific handler for this given type
                else if (handler is RendererToHandlerShim shim)
                {
                    renderer = shim.VisualElementRenderer;

                    if (renderer == null)
                    {
                        renderer = Registrar.Registered.GetHandlerForObject <IVisualElementRenderer>(element, context)
                                   ?? new DefaultRenderer(context);
                    }
                }
                else if (handler is IVisualElementRenderer ver)
                {
                    renderer = ver;
                }
                else if (handler is INativeViewHandler vh)
                {
                    renderer = new HandlerToRendererShim(vh);
                }
            }

            renderer.SetElement(element);
            return(renderer);
        }
Example #2
0
        internal static IVisualElementRenderer CreateRenderer(
            VisualElement element,
            Context context,
            AndroidX.Fragment.App.FragmentManager fragmentManager = null,
            global::Android.Views.LayoutInflater layoutInflater   = null)
        {
            IVisualElementRenderer renderer = null;

            // temporary hack to fix the following issues
            // https://github.com/xamarin/Microsoft.Maui.Controls.Compatibility/issues/13261
            // https://github.com/xamarin/Microsoft.Maui.Controls.Compatibility/issues/12484
            if (element is RadioButton tv && tv.ResolveControlTemplate() != null)
            {
                renderer = new DefaultRenderer(context);
            }

            // This code is duplicated across all platforms currently
            // So if any changes are made here please make sure to apply them to other platform.cs files
            if (renderer == null)
            {
                IViewHandler handler = null;

                //TODO: Handle this with AppBuilderHost

                if (Forms.MauiContext?.Handlers == null)
                {
                    throw new InvalidOperationException("Forms.MauiContext.Handlers cannot be null here");
                }

                try
                {
                    var mauiContext = Forms.MauiContext;

                    if (fragmentManager != null || layoutInflater != null)
                    {
                        mauiContext = mauiContext.MakeScoped(layoutInflater, fragmentManager);
                    }

                    handler = mauiContext.Handlers.GetHandler(element.GetType()) as IViewHandler;
                    handler.SetMauiContext(mauiContext);
                }
                catch (Exception e)
                {
                    Microsoft.Extensions.Logging.LoggerExtensions
                    .LogWarning(Forms.MauiContext.CreateLogger <Platform>(), $"{e}");

                    // TODO define better catch response or define if this is needed?
                }

                if (handler == null)
                {
                    renderer = Registrar.Registered.GetHandlerForObject <IVisualElementRenderer>(element, context)
                               ?? new DefaultRenderer(context);
                }
                // This means the only thing registered is the RendererToHandlerShim
                // Which is only used when you are running a .NET MAUI app
                // This indicates that the user hasn't registered a specific handler for this given type
                else if (handler is RendererToHandlerShim shim)
                {
                    renderer = shim.VisualElementRenderer;

                    if (renderer == null)
                    {
                        renderer = Registrar.Registered.GetHandlerForObject <IVisualElementRenderer>(element, context)
                                   ?? new DefaultRenderer(context);
                    }
                }
                else if (handler is IVisualElementRenderer ver)
                {
                    renderer = ver;
                }
                else if (handler is IPlatformViewHandler vh)
                {
                    renderer        = new HandlerToRendererShim(vh);
                    element.Handler = handler;
                    SetRenderer(element, renderer);
                }
            }

            renderer.SetElement(element);

            if (fragmentManager != null)
            {
                var managesFragments = renderer as IManageFragments;
                managesFragments?.SetFragmentManager(fragmentManager);
            }

            return(renderer);
        }