Skip to content

Commit a10011d

Browse files
MAUI Reactor App sample
1 parent 2198b8f commit a10011d

39 files changed

Lines changed: 1204 additions & 0 deletions

samples/Clock/Clock.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<InternalsVisibleTo Include="BlazorWebAssemblyApp"/>
1717
<InternalsVisibleTo Include="BlazorServerApp"/>
1818
<InternalsVisibleTo Include="MAUIApp"/>
19+
<InternalsVisibleTo Include="MAUIReactorApp"/>
1920
</ItemGroup>
2021

2122
</Project>

samples/MAUIReactorApp/App.xaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<local:MauiReactorApplication xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:MAUIReactorApp"
5+
x:Class="MAUIReactorApp.App">
6+
<local:MauiReactorApplication.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</local:MauiReactorApplication.Resources>
14+
</local:MauiReactorApplication>

samples/MAUIReactorApp/App.xaml.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using MauiReactor;
2+
using MAUIReactorApp.Components;
3+
using MAUIReactorApp.Resources.Styles;
4+
5+
namespace MAUIReactorApp;
6+
7+
public partial class App
8+
{
9+
public App(IServiceProvider serviceProvider)
10+
: base(serviceProvider) =>
11+
InitializeComponent();
12+
}
13+
14+
public abstract class MauiReactorApplication : ReactorApplication<HomePage>
15+
{
16+
protected MauiReactorApplication(IServiceProvider serviceProvider)
17+
: base(serviceProvider) =>
18+
this.UseTheme<ApplicationTheme>();
19+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
2+
namespace MAUIReactorApp.Components;
3+
4+
using System.ComponentModel;
5+
6+
public class HomePageState
7+
{
8+
public int Counter { get; set; }
9+
}
10+
11+
public class HomePage : Component<HomePageState>
12+
{
13+
[Dependency] public IClockViewModel ClockViewModel { get; set; }
14+
15+
public HomePage()
16+
{
17+
Composition.Shared.BuildUp(this);
18+
if (ClockViewModel is INotifyPropertyChanged model)
19+
{
20+
model.PropertyChanged += (_, _) => Invalidate();
21+
}
22+
}
23+
24+
public override VisualNode Render()
25+
{
26+
return ContentPage(
27+
ScrollView(
28+
VStack(
29+
Image("dotnet_bot.png")
30+
.HeightRequest(200)
31+
.HCenter()
32+
.Set(SemanticProperties.DescriptionProperty, "Cute dot net bot waving hi to you!"),
33+
Label($"{ClockViewModel.Date} {ClockViewModel.Time}")
34+
.FontSize(32)
35+
.HCenter(),
36+
Label("Welcome to MauiReactor Pure.DI example!")
37+
.FontSize(18)
38+
.HCenter(),
39+
Button(State.Counter == 0 ? "Click me" : $"Clicked {State.Counter} times!")
40+
.OnClicked(() => SetState(s => s.Counter += 1))
41+
.HCenter()
42+
)
43+
.VCenter()
44+
.Spacing(25)
45+
.Padding(30, 0)
46+
)
47+
);
48+
}
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ReSharper disable UnusedMember.Local
2+
// ReSharper disable UnusedMember.Global
3+
// ReSharper disable RedundantNameQualifier
4+
namespace MAUIReactorApp;
5+
6+
using Clock.ViewModels;
7+
using Components;
8+
using Pure.DI;
9+
using Pure.DI.MS;
10+
using static Pure.DI.Lifetime;
11+
using Timer = Clock.Models.Timer;
12+
13+
partial class Composition: ServiceProviderFactory<Composition>
14+
{
15+
public static readonly Composition Shared = new();
16+
17+
private static void Setup() => DI.Setup()
18+
.DependsOn(Base)
19+
// Specifies not to attempt to resolve types whose fully qualified name
20+
// begins with Microsoft.Extensions., Microsoft.Maui.
21+
// since ServiceProvider will be used to retrieve them.
22+
.Hint(Hint.OnCannotResolveContractTypeNameWildcard, "Microsoft.Extensions.*")
23+
.Hint(Hint.OnCannotResolveContractTypeNameWildcard, "Microsoft.Maui.*")
24+
25+
// Builders
26+
.Builder<HomePage>()
27+
28+
// View Models
29+
.Bind().As(Singleton).To<ClockViewModel>()
30+
31+
// Models
32+
.Bind().To<Log<TT>>()
33+
.Bind().As(Singleton).To<Timer>()
34+
.Bind().As(PerBlock).To<SystemClock>()
35+
36+
// Infrastructure
37+
.Bind().To<Dispatcher>();
38+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace MAUIReactorApp;
2+
3+
using IDispatcher = Microsoft.Maui.Dispatching.IDispatcher;
4+
5+
// ReSharper disable once ClassNeverInstantiated.Global
6+
class Dispatcher(IDispatcher dispatcher)
7+
: Clock.ViewModels.IDispatcher
8+
{
9+
public void Dispatch(Action action) =>
10+
dispatcher.Dispatch(action);
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
global using System;
2+
global using Clock.Models;
3+
global using Clock.ViewModels;
4+
global using Pure.DI;
5+
global using Microsoft.Maui;
6+
global using Microsoft.Maui.Hosting;
7+
global using Microsoft.Maui.Graphics;
8+
global using MauiReactor;
9+
global using DependencyAttribute = Pure.DI.DependencyAttribute;

samples/MAUIReactorApp/Log.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ReSharper disable TemplateIsNotCompileTimeConstantProblem
2+
// ReSharper disable ContextualLoggerProblem
3+
namespace MAUIReactorApp;
4+
5+
using Microsoft.Extensions.Logging;
6+
7+
class Log<T>(ILogger<T> logger) : ILog<T>
8+
{
9+
public void Info(string message) =>
10+
logger.LogInformation(message);
11+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>$(BaseTargetFramework)-android;$(BaseTargetFramework)-ios;$(BaseTargetFramework)-maccatalyst</TargetFrameworks>
5+
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);$(BaseTargetFramework)-windows10.0.19041.0</TargetFrameworks>
6+
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
7+
<!-- <TargetFrameworks>$(TargetFrameworks);net9.0-tizen</TargetFrameworks> -->
8+
9+
<!-- Note for MacCatalyst:
10+
The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
11+
When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
12+
The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
13+
either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
14+
<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->
15+
16+
<OutputType>Exe</OutputType>
17+
<RootNamespace>MAUIReactorApp</RootNamespace>
18+
<UseMaui>true</UseMaui>
19+
<SingleProject>true</SingleProject>
20+
<ImplicitUsings>enable</ImplicitUsings>
21+
<Nullable>enable</Nullable>
22+
23+
<!-- Display name -->
24+
<ApplicationTitle>MAUIReactorApp</ApplicationTitle>
25+
26+
<!-- App Identifier -->
27+
<ApplicationId>com.companyname.mauireactorapp</ApplicationId>
28+
29+
<!-- Versions -->
30+
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
31+
<ApplicationVersion>1</ApplicationVersion>
32+
33+
<!-- To develop, package, and publish an app to the Microsoft Store, see: https://aka.ms/MauiTemplateUnpackaged -->
34+
<WindowsPackageType>None</WindowsPackageType>
35+
36+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.0</SupportedOSPlatformVersion>
37+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">15.0</SupportedOSPlatformVersion>
38+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
39+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
40+
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
41+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
42+
</PropertyGroup>
43+
44+
<ItemGroup>
45+
<!-- App Icon -->
46+
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4"/>
47+
48+
<!-- Splash Screen -->
49+
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128"/>
50+
51+
<!-- Images -->
52+
<MauiImage Include="Resources\Images\*"/>
53+
<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185"/>
54+
55+
<!-- Custom Fonts -->
56+
<MauiFont Include="Resources\Fonts\*"/>
57+
58+
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
59+
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)"/>
60+
</ItemGroup>
61+
62+
<ItemGroup Condition="'$(Configuration)'=='Debug'">
63+
<RuntimeHostConfigurationOption Include="MauiReactor.HotReload" Value="true" Trim="false"/>
64+
</ItemGroup>
65+
<ItemGroup Condition="'$(Configuration)|$(TargetFramework)'=='Release|net9.0-ios'">
66+
<RuntimeHostConfigurationOption Include="MauiReactor.HotReload" Value="false" Trim="true"/>
67+
</ItemGroup>
68+
69+
<ItemGroup>
70+
<PackageReference Include="Microsoft.Maui.Controls" Version="9.0.21"/>
71+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0"/>
72+
<PackageReference Include="Reactor.Maui" Version="3.0.*"/>
73+
<ProjectReference Include="..\..\src\Pure.DI.Core\Pure.DI.Core.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
74+
<ProjectReference Include="..\..\src\Pure.DI\Pure.DI.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
75+
<ProjectReference Include="..\Clock\Clock.csproj"/>
76+
<Compile Include="..\..\src\Pure.DI.MS\any\Pure.DI\MS\*.cs" Link=""/>
77+
</ItemGroup>
78+
79+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
namespace MAUIReactorApp;
4+
5+
public static class MauiProgram
6+
{
7+
public static MauiApp CreateMauiApp()
8+
{
9+
var builder = MauiApp.CreateBuilder();
10+
11+
// Uses Composition as an alternative IServiceProviderFactory
12+
using (Composition.Shared)
13+
{
14+
builder.ConfigureContainer(Composition.Shared);
15+
16+
builder
17+
// .UseMauiApp(serviceProvider => new App(serviceProvider))
18+
.UseMauiApp<App>()
19+
.ConfigureFonts(fonts => {
20+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
21+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
22+
});
23+
24+
return builder.Build();
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)