diff --git a/src/BlazorBindings.Maui/MauiBlazorBindingsRenderer.cs b/src/BlazorBindings.Maui/MauiBlazorBindingsRenderer.cs index e967a17a..524f430a 100644 --- a/src/BlazorBindings.Maui/MauiBlazorBindingsRenderer.cs +++ b/src/BlazorBindings.Maui/MauiBlazorBindingsRenderer.cs @@ -43,41 +43,9 @@ public Task AddComponent(Type componentType, MC.Application parent, Dictionary AddComponent(MC.Element parent, Dictionary parameters = null) where TComponent : IComponent + public Task AddComponent(MC.Application parent, Dictionary parameters = null) { - var task = AddComponent(typeof(TComponent), parent, parameters); - return Cast(task); - - static async Task Cast(Task task) => (TComponent)await task; - } - - public Task AddComponent(Type componentType, MC.Element parent, Dictionary parameters = null) - { - var componentTask = AddComponentLocal(); - - if (componentTask.Exception != null) - { - // If exception was thrown during the sync execution - throw it straight away. - ExceptionDispatchInfo.Throw(componentTask.Exception.InnerException); - } - - return componentTask; - - async Task AddComponentLocal() - { - var elementsComponentTask = GetElementsFromRenderedComponent(componentType, parameters); - - if (!elementsComponentTask.IsCompleted && parent is MC.Application app) - { - // MAUI requires the Application to have the MainPage. If rendering task is not completed synchronously, - // we need to set MainPage to something. - app.MainPage ??= new MC.ContentPage(); - } - - var (elements, componentTask) = await elementsComponentTask; - await SetChildContent(parent, elements); - return await componentTask; - } + return AddComponent(typeof(T), parent, parameters); } protected override void HandleException(Exception exception) @@ -120,69 +88,5 @@ protected override ElementManager CreateNativeControlManager() return (container.Elements, addComponentTask); } - - private static Task SetChildContent(MC.BindableObject parent, List children) - { - switch (parent) - { - case MC.NavigationPage page: - return page.PushAsync(CastSingle(children)); - - case MC.Application application: - application.MainPage = CastSingle(children); - break; - case MC.ContentPage contentPage: - contentPage.Content = CastSingle(children); - break; - case MC.ContentView contentView: - contentView.Content = CastSingle(children); - break; - case MC.FlyoutPage flyoutPage: - flyoutPage.Detail = CastSingle(children); - break; - case MC.ScrollView scrollView: - scrollView.Content = CastSingle(children); - break; - case MC.ShellContent shellContent: - shellContent.Content = CastSingle(children); - break; - case MC.StackBase stackBase: - foreach (var child in children) - stackBase.Children.Add(Cast(child)); - break; - case MC.TabbedPage tabbedPage: - foreach (var child in children) - tabbedPage.Children.Add(Cast(child)); - break; - case MC.Shell shell: - foreach (var child in children) - shell.Items.Add(Cast(child)); - break; - case MC.ShellItem shellItem: - foreach (var child in children) - shellItem.Items.Add(Cast(child)); - break; - case MC.ShellSection shellSection: - foreach (var child in children) - shellSection.Items.Add(Cast(child)); - break; - - default: - throw new InvalidOperationException($"Renderer doesn't support {parent?.GetType()?.Name} as a parent element."); - }; - - return Task.CompletedTask; - - static T CastSingle(List children) where T : MC.BindableObject - { - if (children.Count != 1) - throw new InvalidOperationException("The target component must have exactly one root element."); - - return Cast(children[0]); - } - - static T Cast(MC.BindableObject e) where T : class - => e as T ?? throw new InvalidOperationException($"{typeof(T).Name} element expected, but {e?.GetType()?.Name} found."); - } } } diff --git a/src/BlazorBindings.UnitTests/Components/ButtonWithAnExceptionOnClick.razor b/src/BlazorBindings.UnitTests/Components/PageWithButtonWithExceptionOnClick.razor similarity index 66% rename from src/BlazorBindings.UnitTests/Components/ButtonWithAnExceptionOnClick.razor rename to src/BlazorBindings.UnitTests/Components/PageWithButtonWithExceptionOnClick.razor index 2153ed1d..a221aba7 100644 --- a/src/BlazorBindings.UnitTests/Components/ButtonWithAnExceptionOnClick.razor +++ b/src/BlazorBindings.UnitTests/Components/PageWithButtonWithExceptionOnClick.razor @@ -1,4 +1,7 @@ -