From 7211e6908f927059037c3ec40b9776d3b37ef73e Mon Sep 17 00:00:00 2001 From: Vijay Anand E G <81947404+egvijayanand@users.noreply.github.com> Date: Wed, 17 Nov 2021 16:06:11 +0530 Subject: [PATCH] Added extension methods for BlazorWebView --- .../MauiBlazorApp/BlazorPage.designer.cs | 12 +++-- .../Extensions/BlazorExtensions.cs | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 src/MauiBlazorApp/MauiBlazorApp/Extensions/BlazorExtensions.cs diff --git a/src/MauiBlazorApp/MauiBlazorApp/BlazorPage.designer.cs b/src/MauiBlazorApp/MauiBlazorApp/BlazorPage.designer.cs index c25c81b..4a11176 100644 --- a/src/MauiBlazorApp/MauiBlazorApp/BlazorPage.designer.cs +++ b/src/MauiBlazorApp/MauiBlazorApp/BlazorPage.designer.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Components.WebView.Maui; using Microsoft.Maui.Controls; using static CommunityToolkit.Maui.Markup.GridRowsColumns; +using MauiBlazorApp.Extensions; namespace MauiBlazorApp { @@ -33,16 +34,13 @@ public void InitializeComponent() .Row(BodyRow.Top), new BlazorWebView() { - HostPage = "wwwroot/index.html", RootComponents = { - new() - { - ComponentType = typeof(Gateway), - Selector = "#app" - }, + new RootComponent().ComponentType(typeof(Gateway)) + .Selector("#app"), }, - }.Row(BodyRow.Bottom), + }.HostPage("wwwroot/index.html") + .Row(BodyRow.Bottom), } }.FillExpand(); } diff --git a/src/MauiBlazorApp/MauiBlazorApp/Extensions/BlazorExtensions.cs b/src/MauiBlazorApp/MauiBlazorApp/Extensions/BlazorExtensions.cs new file mode 100644 index 0000000..79aaf17 --- /dev/null +++ b/src/MauiBlazorApp/MauiBlazorApp/Extensions/BlazorExtensions.cs @@ -0,0 +1,44 @@ +using Microsoft.AspNetCore.Components.WebView.Maui; +using System; +using System.Collections.Generic; + +namespace MauiBlazorApp.Extensions +{ + public static class BlazorExtensions + { + public static TBlazorWebView HostPage(this TBlazorWebView blazorWV, string hostPage) + where TBlazorWebView : BlazorWebView + { + blazorWV.HostPage = hostPage; + return blazorWV; + } + + public static TRootComponent ComponentType(this TRootComponent component, Type type) + where TRootComponent : RootComponent + { + component.ComponentType = type; + return component; + } + + public static TRootComponent Selector(this TRootComponent component, string selector) + where TRootComponent : RootComponent + { + component.Selector = selector; + return component; + } + + public static TRootComponent Parameters(this TRootComponent component, params (string key, object? value)[] parameters) + where TRootComponent : RootComponent + { + var dict = new Dictionary(); + + foreach (var (key, value) in parameters) + { + dict.Add(key, value); + } + + component.Parameters = dict; + return component; + } + } +}