From 991f1eb57714bacab5a65b4729aa9bff51295dca Mon Sep 17 00:00:00 2001 From: "Kevin E. Ford" <4380198+Bowman74@users.noreply.github.com.> Date: Thu, 26 Dec 2024 23:43:54 -0700 Subject: [PATCH] #3785 Update to MAUI sample for CSLA 9. This also fixes several design issues for how a MAUI application should use dependency injection and navigation. The service locator has been removed. Fixing the issue with the navigation also fixed the issue with how dependency injection should be done. --- .../BusinessLibrary/BusinessLibrary.csproj | 6 +-- .../MauiExample/DataAccess/DataAccess.csproj | 4 +- Samples/MauiExample/MauiExample/App.xaml.cs | 17 +++++-- Samples/MauiExample/MauiExample/Constants.cs | 14 ++++++ .../MauiExample/MauiExample.csproj | 47 ++++++++++++------- .../MauiExample/MauiExample/MauiProgram.cs | 16 +++++-- .../MauiExample/Pages/PersonEditPage.xaml | 14 +++--- .../MauiExample/Pages/PersonEditPage.xaml.cs | 33 ++++++++----- .../MauiExample/Pages/PersonListPage.xaml | 7 +-- .../MauiExample/Pages/PersonListPage.xaml.cs | 23 +++++---- 10 files changed, 121 insertions(+), 60 deletions(-) create mode 100644 Samples/MauiExample/MauiExample/Constants.cs diff --git a/Samples/MauiExample/BusinessLibrary/BusinessLibrary.csproj b/Samples/MauiExample/BusinessLibrary/BusinessLibrary.csproj index 15b777e4da..15d127995e 100644 --- a/Samples/MauiExample/BusinessLibrary/BusinessLibrary.csproj +++ b/Samples/MauiExample/BusinessLibrary/BusinessLibrary.csproj @@ -1,13 +1,13 @@ - + - net6.0 + net9.0 enable enable - + diff --git a/Samples/MauiExample/DataAccess/DataAccess.csproj b/Samples/MauiExample/DataAccess/DataAccess.csproj index d215c71f27..79b2243426 100644 --- a/Samples/MauiExample/DataAccess/DataAccess.csproj +++ b/Samples/MauiExample/DataAccess/DataAccess.csproj @@ -1,7 +1,7 @@ - + - net6.0 + net9.0 enable diff --git a/Samples/MauiExample/MauiExample/App.xaml.cs b/Samples/MauiExample/MauiExample/App.xaml.cs index 11c074a391..a4d68061c8 100644 --- a/Samples/MauiExample/MauiExample/App.xaml.cs +++ b/Samples/MauiExample/MauiExample/App.xaml.cs @@ -1,4 +1,4 @@ -using Csla; +using MauiExample.Pages; namespace MauiExample; @@ -7,9 +7,18 @@ public partial class App : Application public App() { InitializeComponent(); + RegisterRoutes(); - MainPage = new AppShell(); - } + } - public static ApplicationContext ApplicationContext { get; set; } + public void RegisterRoutes() + { + Routing.RegisterRoute(Constants.PersonEditRoute, typeof(PersonEditPage)); + Routing.RegisterRoute(Constants.PersonListRoute, typeof(PersonListPage)); + } + + protected override Window CreateWindow(IActivationState? activationState) + { + return new Window(new AppShell()); + } } diff --git a/Samples/MauiExample/MauiExample/Constants.cs b/Samples/MauiExample/MauiExample/Constants.cs new file mode 100644 index 0000000000..17695467c0 --- /dev/null +++ b/Samples/MauiExample/MauiExample/Constants.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MauiExample; +internal static class Constants +{ + public const string PersonEditRoute = "PersonEditRoute"; + public const string PersonListRoute = "PersonListRoute"; + + public const string PersonIdParameter = "PersonId"; +} diff --git a/Samples/MauiExample/MauiExample/MauiExample.csproj b/Samples/MauiExample/MauiExample/MauiExample.csproj index 6a37ab6d5f..a3555314e7 100644 --- a/Samples/MauiExample/MauiExample/MauiExample.csproj +++ b/Samples/MauiExample/MauiExample/MauiExample.csproj @@ -1,8 +1,8 @@  - net8.0-android;net8.0-ios;net8.0-maccatalyst - $(TargetFrameworks);net8.0-windows10.0.19041.0 + net9.0-android;net9.0-ios;net9.0-maccatalyst + $(TargetFrameworks);net9.0-windows10.0.19041.0 Exe @@ -10,7 +10,9 @@ true true enable - + enable + true + MauiExample @@ -22,43 +24,55 @@ 1.0 1 - 14.2 - 14.0 + 15.0 + 15.0 21.0 10.0.17763.0 10.0.17763.0 6.5 - + + + + + + + + + + automatic + + + $(DefineConstants)TRACE;MAUI - + $(DefineConstants)TRACE;MAUI - + $(DefineConstants)TRACE;MAUI - + $(DefineConstants)TRACE;MAUI - + $(DefineConstants)TRACE;MAUI - + $(DefineConstants)TRACE;MAUI - + $(DefineConstants)TRACE;MAUI - + $(DefineConstants)TRACE;MAUI @@ -84,8 +98,10 @@ - - + + + + @@ -100,5 +116,4 @@ MSBuild:Compile - diff --git a/Samples/MauiExample/MauiExample/MauiProgram.cs b/Samples/MauiExample/MauiExample/MauiProgram.cs index 8800a792c5..3354cca848 100644 --- a/Samples/MauiExample/MauiExample/MauiProgram.cs +++ b/Samples/MauiExample/MauiExample/MauiProgram.cs @@ -1,5 +1,5 @@ -using Csla; -using Csla.Configuration; +using Csla.Configuration; +using MauiExample.ViewModels; namespace MauiExample; @@ -16,13 +16,19 @@ public static MauiApp CreateMauiApp() fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); }); - builder.Services.AddTransient(); builder.Services.AddCsla(options => options .AddXaml()); - var provider = builder.Services.BuildServiceProvider(); - App.ApplicationContext = provider.GetRequiredService(); + AddDependancyInjection(builder); return builder.Build(); } + + private static void AddDependancyInjection(MauiAppBuilder builder) + { + builder.Services.AddTransient(); + + builder.Services.AddScoped(); + builder.Services.AddScoped(); + } } diff --git a/Samples/MauiExample/MauiExample/Pages/PersonEditPage.xaml b/Samples/MauiExample/MauiExample/Pages/PersonEditPage.xaml index ee13bf308f..70c1a1580a 100644 --- a/Samples/MauiExample/MauiExample/Pages/PersonEditPage.xaml +++ b/Samples/MauiExample/MauiExample/Pages/PersonEditPage.xaml @@ -2,6 +2,8 @@ @@ -13,7 +15,7 @@ Margin="10,0,0,10" SemanticProperties.HeadingLevel="Level1"/> - + @@ -25,14 +27,14 @@