Skip to content

Commit

Permalink
Merge pull request #8 from egvijayanand/working
Browse files Browse the repository at this point in the history
Logic moved to Razor Class Library and Shared AppState with .NET MAUI
  • Loading branch information
egvijayanand authored Nov 16, 2021
2 parents 9bc69d9 + c941d1e commit d380c09
Show file tree
Hide file tree
Showing 25 changed files with 361 additions and 64 deletions.
8 changes: 7 additions & 1 deletion src/MauiBlazorApp/MauiBlazorApp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.31903.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MauiBlazorApp", "MauiBlazorApp\MauiBlazorApp.csproj", "{6001DD47-2BA1-48BF-9992-4A768EA19253}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MauiBlazorApp", "MauiBlazorApp\MauiBlazorApp.csproj", "{6001DD47-2BA1-48BF-9992-4A768EA19253}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RazorLib", "RazorLib\RazorLib.csproj", "{3AB50B6C-22E6-4B7E-AADB-0F569ED7A0F6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -16,6 +18,10 @@ Global
{6001DD47-2BA1-48BF-9992-4A768EA19253}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{6001DD47-2BA1-48BF-9992-4A768EA19253}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6001DD47-2BA1-48BF-9992-4A768EA19253}.Release|Any CPU.Build.0 = Release|Any CPU
{3AB50B6C-22E6-4B7E-AADB-0F569ED7A0F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3AB50B6C-22E6-4B7E-AADB-0F569ED7A0F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3AB50B6C-22E6-4B7E-AADB-0F569ED7A0F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3AB50B6C-22E6-4B7E-AADB-0F569ED7A0F6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 4 additions & 2 deletions src/MauiBlazorApp/MauiBlazorApp/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
<Color x:Key="PrimaryColor">#512BDF</Color>
<Color x:Key="SecondaryColor">White</Color>

<Style TargetType="Label">
<Style TargetType="Label"
ApplyToDerivedTypes="True">
<Setter Property="TextColor"
Value="{DynamicResource PrimaryColor}" />
<Setter Property="FontFamily"
Value="OpenSansRegular" />
</Style>

<Style TargetType="Button">
<Style TargetType="Button"
ApplyToDerivedTypes="True">
<Setter Property="TextColor"
Value="{DynamicResource SecondaryColor}" />
<Setter Property="FontFamily"
Expand Down
7 changes: 4 additions & 3 deletions src/MauiBlazorApp/MauiBlazorApp/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Maui;
using BlazorApp.Services;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
using Application = Microsoft.Maui.Controls.Application;
Expand All @@ -12,10 +13,10 @@ public App()
InitializeComponent();

// C# definition
MainPage = new BlazorPage();
MainPage = AppService.GetService<BlazorPage>();

// XAML definition
//MainPage = new WebPage();
//MainPage = AppService.GetService<WebPage>();
}
}
}
37 changes: 22 additions & 15 deletions src/MauiBlazorApp/MauiBlazorApp/BlazorPage.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
using Microsoft.AspNetCore.Components.WebView.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls;
using RazorLib;
using System;

namespace MauiBlazorApp
{
public class BlazorPage : ContentPage
public partial class BlazorPage : ContentPage, IDisposable
{
public BlazorPage()
private readonly AppState _state;

public BlazorPage(AppState appState)
{
SetDynamicResource(BackgroundColorProperty, "SecondaryColor");
InitializeComponent();
_state = appState;
_state.OnChanged += OnStateChanged;
}

var blazor = new BlazorWebView()
{
HostPage = "wwwroot/index.html"
};
private void OnStateChanged()
{
lblCounter.Text = $"The current count is: {_state.CurrentCount}";
}

blazor.RootComponents.Add(new()
{
ComponentType = typeof(Gateway),
Selector = "#app"
});
private void Counter_Clicked(object sender, EventArgs e)
{
_state.IncrementCount();
}

Content = blazor;
public void Dispose()
{
_state.OnChanged -= OnStateChanged;
}
}
}
54 changes: 54 additions & 0 deletions src/MauiBlazorApp/MauiBlazorApp/BlazorPage.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/MauiBlazorApp/MauiBlazorApp/Controls/TextButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Maui.Controls;

namespace MauiBlazorApp.Controls
{
public class TextButton : Button
{
public TextButton(string text)
{
Text = text;
}
}
}
12 changes: 12 additions & 0 deletions src/MauiBlazorApp/MauiBlazorApp/Controls/TextLabel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Maui.Controls;

namespace MauiBlazorApp.Controls
{
public class TextLabel : Label
{
public TextLabel(string text)
{
Text = text;
}
}
}
15 changes: 0 additions & 15 deletions src/MauiBlazorApp/MauiBlazorApp/Counter.razor

This file was deleted.

6 changes: 3 additions & 3 deletions src/MauiBlazorApp/MauiBlazorApp/Gateway.razor
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Router AppAssembly="@GetType().Assembly">
<Router AppAssembly="@GetType().Assembly" AdditionalAssemblies="new[] { typeof(Counter).Assembly }">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
<LayoutView Layout="@typeof(ErrorLayout)">
<p class="validation-message">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
3 changes: 0 additions & 3 deletions src/MauiBlazorApp/MauiBlazorApp/Index.razor

This file was deleted.

25 changes: 19 additions & 6 deletions src/MauiBlazorApp/MauiBlazorApp/MauiBlazorApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@

<ItemGroup>
<!-- App Icon -->
<MauiImage Include="Resources\appicon.svg" ForegroundFile="Resources\appiconfg.svg" IsAppIcon="true" Color="#512BD4" />
<MauiImage Include="Resources\appicon.svg"
ForegroundFile="Resources\appiconfg.svg"
IsAppIcon="true"
Color="#512BD4" />

<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\appiconfg.svg" Color="#512BD4" />
<MauiSplashScreen Include="Resources\appiconfg.svg"
Color="#512BD4" />

<!-- Images -->
<MauiImage Include="Resources\Images\*" />
Expand All @@ -45,18 +49,27 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="6.0.0" />
<PackageReference Include="CommunityToolkit.Maui.Markup"
Version="1.0.0-pre2" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded"
Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.Contains('-windows'))">
<!-- Required - WinUI does not yet have buildTransitive for everything -->
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.0-preview3" />
<PackageReference Include="Microsoft.Graphics.Win2D" Version="1.0.0.29-preview3" />
<PackageReference Include="Microsoft.WindowsAppSDK"
Version="1.0.0-preview3" />
<PackageReference Include="Microsoft.Graphics.Win2D"
Version="1.0.0.29-preview3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\RazorLib\RazorLib.csproj" />
</ItemGroup>

<ItemGroup>
<MauiXaml Update="App.xaml">
<Generator>MSBuild:Compile</Generator>
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="WebPage.xaml">
<Generator>MSBuild:Compile</Generator>
Expand Down
5 changes: 5 additions & 0 deletions src/MauiBlazorApp/MauiBlazorApp/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
using RazorLib;

namespace MauiBlazorApp
{
Expand All @@ -20,6 +21,10 @@ public static MauiApp CreateMauiApp()

builder.Services.AddBlazorWebView();

builder.Services.AddSingleton<AppState>();
builder.Services.AddSingleton<BlazorPage>();
builder.Services.AddSingleton<WebPage>();

return builder.Build();
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/MauiBlazorApp/MauiBlazorApp/Services/AppService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using System;

namespace BlazorApp.Services
{
public static class AppService
{
public static TService GetService<TService>() => Current.GetService<TService>();

public static IServiceProvider Current =>
#if ANDROID
MauiApplication.Current.Services;
#elif IOS || MACCATALYST
MauiUIApplicationDelegate.Current.Services;
#elif WINDOWS10_0_17763_0_OR_GREATER
MauiWinUIApplication.Current.Services;
#else
null;
#endif
}
}
27 changes: 21 additions & 6 deletions src/MauiBlazorApp/MauiBlazorApp/WebPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@
xmlns:local="clr-namespace:MauiBlazorApp"
BackgroundColor="{DynamicResource SecondaryColor}">

<b:BlazorWebView HostPage="wwwroot/index.html">
<b:BlazorWebView.RootComponents>
<b:RootComponent ComponentType="{x:Type local:Gateway}"
Selector="#app" />
</b:BlazorWebView.RootComponents>
</b:BlazorWebView>
<Grid RowDefinitions="Auto, *"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<StackLayout Grid.Row="0"
Padding="20">
<Label x:Name="lblCounter"
HorizontalOptions="StartAndExpand"
VerticalOptions="Center" />
<Button Text="Increment"
Clicked="Counter_Clicked"
HorizontalOptions="End"
VerticalOptions="Center" />
</StackLayout>
<b:BlazorWebView Grid.Row="1"
HostPage="wwwroot/index.html">
<b:BlazorWebView.RootComponents>
<b:RootComponent ComponentType="{x:Type local:Gateway}"
Selector="#app" />
</b:BlazorWebView.RootComponents>
</b:BlazorWebView>
</Grid>

</ContentPage>
31 changes: 23 additions & 8 deletions src/MauiBlazorApp/MauiBlazorApp/WebPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
using RazorLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MauiBlazorApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class WebPage : ContentPage
public partial class WebPage : ContentPage, IDisposable
{
public WebPage()
private readonly AppState _state;

public WebPage(AppState appState)
{
InitializeComponent();
_state = appState;
_state.OnChanged += OnStateChanged;
}

private void OnStateChanged()
{
lblCounter.Text = $"The current count is: {_state.CurrentCount}";
}

private void Counter_Clicked(object sender, EventArgs e)
{
_state.IncrementCount();
}

public void Dispose()
{
_state.OnChanged -= OnStateChanged;
}
}
}
Loading

0 comments on commit d380c09

Please sign in to comment.