From 5bafabcdf1859c734919f0f1bd36091ea0b4d486 Mon Sep 17 00:00:00 2001 From: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Date: Wed, 21 Feb 2024 09:22:27 -0800 Subject: [PATCH] Refactor Sample --- CommunityMauiMediaElement/App.xaml.cs | 4 +- CommunityMauiMediaElement/AppShell.xaml | 5 -- CommunityMauiMediaElement/AppShell.xaml.cs | 45 ++++++++++++----- CommunityMauiMediaElement/MainPage.xaml.cs | 49 +++++++++++-------- CommunityMauiMediaElement/MauiProgram.cs | 4 ++ .../ViewModels/MyViewModel.cs | 18 +------ .../Views/MyPage.xaml.cs | 39 ++++++++------- .../Views/MyThirdPage.xaml | 2 + .../Views/MyThirdPage.xaml.cs | 20 ++++++++ 9 files changed, 112 insertions(+), 74 deletions(-) diff --git a/CommunityMauiMediaElement/App.xaml.cs b/CommunityMauiMediaElement/App.xaml.cs index 92a1865..0748a5f 100644 --- a/CommunityMauiMediaElement/App.xaml.cs +++ b/CommunityMauiMediaElement/App.xaml.cs @@ -2,11 +2,11 @@ { public partial class App : Application { - public App() + public App(AppShell appShell) { InitializeComponent(); - MainPage = new AppShell(); + MainPage = appShell; } } } diff --git a/CommunityMauiMediaElement/AppShell.xaml b/CommunityMauiMediaElement/AppShell.xaml index 28df62b..04fae43 100644 --- a/CommunityMauiMediaElement/AppShell.xaml +++ b/CommunityMauiMediaElement/AppShell.xaml @@ -7,9 +7,4 @@ Shell.FlyoutBehavior="Disabled" Title="CommunityMauiMediaElement"> - - diff --git a/CommunityMauiMediaElement/AppShell.xaml.cs b/CommunityMauiMediaElement/AppShell.xaml.cs index cb37c2e..ec34c16 100644 --- a/CommunityMauiMediaElement/AppShell.xaml.cs +++ b/CommunityMauiMediaElement/AppShell.xaml.cs @@ -2,14 +2,37 @@ namespace CommunityMauiMediaElement { - public partial class AppShell : Shell - { - public AppShell() - { - InitializeComponent(); - - Routing.RegisterRoute(nameof(MyPage), typeof(MyPage)); - Routing.RegisterRoute(nameof(MyThirdPage), typeof(MyThirdPage)); - } - } -} + public partial class AppShell : Shell + { + public AppShell(MainPage mainPage) + { + InitializeComponent(); + + Items.Add(mainPage); + + Routing.RegisterRoute(GetRoute(), typeof(MainPage)); + Routing.RegisterRoute(GetRoute(), typeof(MyPage)); + Routing.RegisterRoute(GetRoute(), typeof(MyThirdPage)); + } + + public static string GetRoute() where T : ContentPage + { + if (typeof(T) == typeof(MainPage)) + { + return $"//{nameof(MainPage)}"; + } + + if (typeof(T) == typeof(MyPage)) + { + return $"//{nameof(MainPage)}/{nameof(MyThirdPage)}/{nameof(MyPage)}"; + } + + if (typeof(T) == typeof(MyThirdPage)) + { + return $"//{nameof(MainPage)}/{nameof(MyThirdPage)}/"; + } + + throw new NotSupportedException(); + } + } +} \ No newline at end of file diff --git a/CommunityMauiMediaElement/MainPage.xaml.cs b/CommunityMauiMediaElement/MainPage.xaml.cs index e5ac935..782d615 100644 --- a/CommunityMauiMediaElement/MainPage.xaml.cs +++ b/CommunityMauiMediaElement/MainPage.xaml.cs @@ -3,26 +3,33 @@ namespace CommunityMauiMediaElement { - public partial class MainPage : ContentPage - { - public MainPage() - { - InitializeComponent(); + public partial class MainPage : ContentPage + { + public MainPage() + { + InitializeComponent(); + } + + protected override void OnAppearing() + { + base.OnAppearing(); + + Trace.WriteLine("Pages in NavigationStack:"); - Loaded += OnLoaded; - } + foreach (var page in Navigation.NavigationStack) + { + Trace.WriteLine($"\t{page?.GetType().FullName ?? "null"}"); + } + + GC.Collect(); + GC.WaitForPendingFinalizers(); + var totalMemory = GC.GetTotalMemory(true); + Trace.WriteLine($"Memory: {totalMemory}"); + } - private void OnLoaded(object? sender, EventArgs e) - { - GC.Collect(); - GC.WaitForPendingFinalizers(); - var totalMemory = GC.GetTotalMemory(true); - Debug.WriteLine($"Memory: {totalMemory}"); - } - - private async void Button_OnClicked(object? sender, EventArgs e) - { - await Shell.Current.GoToAsync(nameof(MyPage)); - } - } -} + private async void Button_OnClicked(object? sender, EventArgs e) + { + await Shell.Current.GoToAsync(AppShell.GetRoute()); + } + } +} \ No newline at end of file diff --git a/CommunityMauiMediaElement/MauiProgram.cs b/CommunityMauiMediaElement/MauiProgram.cs index 8f15d1f..11e7571 100644 --- a/CommunityMauiMediaElement/MauiProgram.cs +++ b/CommunityMauiMediaElement/MauiProgram.cs @@ -23,6 +23,10 @@ public static MauiApp CreateMauiApp() builder.Logging.AddDebug(); #endif + builder.Services.AddSingleton(); + builder.Services.AddSingleton(FileSystem.Current); + + builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); diff --git a/CommunityMauiMediaElement/ViewModels/MyViewModel.cs b/CommunityMauiMediaElement/ViewModels/MyViewModel.cs index 7fc64e9..4822493 100644 --- a/CommunityMauiMediaElement/ViewModels/MyViewModel.cs +++ b/CommunityMauiMediaElement/ViewModels/MyViewModel.cs @@ -1,20 +1,6 @@ namespace CommunityMauiMediaElement.ViewModels; -public class MyViewModel +public class MyViewModel(IFileSystem fileSystem) { - public string FilePath => Path.Combine(BaseFolderPath, "Sister.m4a"); - - private string BaseFolderPath - { - get - { -#if IOS || MACCATALYST - return Foundation.NSBundle.MainBundle.BundlePath; -#elif WINDOWS10_0_17763_0_OR_GREATER - return AppDomain.CurrentDomain.BaseDirectory; -#else - throw new NotImplementedException(); -#endif - } - } + public string FilePath => Path.Combine(fileSystem.AppDataDirectory, "Sister.m4a"); } \ No newline at end of file diff --git a/CommunityMauiMediaElement/Views/MyPage.xaml.cs b/CommunityMauiMediaElement/Views/MyPage.xaml.cs index f5181f1..44de17c 100644 --- a/CommunityMauiMediaElement/Views/MyPage.xaml.cs +++ b/CommunityMauiMediaElement/Views/MyPage.xaml.cs @@ -5,40 +5,41 @@ namespace CommunityMauiMediaElement.Views; public partial class MyPage : ContentPage { - public MyPage(MyViewModel viewModel) + public MyPage() { InitializeComponent(); - BindingContext = viewModel; + Trace.WriteLine($"{this.GetType().FullName} Initialized"); + Unloaded += OnUnloaded; } - - private void OnUnloaded(object? sender, EventArgs e) + + ~MyPage() { - Debug.WriteLine("OnUnloaded() called"); - MyMediaElement.Pause(); + Trace.WriteLine($"{this.GetType().FullName} Disposed"); } - protected override void OnHandlerChanging(HandlerChangingEventArgs args) + protected override void OnAppearing() { - base.OnHandlerChanging(args); - - if (args.NewHandler != null) - { - return; - } + base.OnAppearing(); + + GC.Collect(); + GC.WaitForPendingFinalizers(); + } + private void OnUnloaded(object? sender, EventArgs e) + { + Debug.WriteLine("OnUnloaded() called"); + MyMediaElement.Pause(); MyMediaElement.Handler?.DisconnectHandler(); Debug.WriteLine("OnHandlerChanging.DisconnectHandler() called"); + + + Unloaded -= OnUnloaded; } private async void Button_OnClicked(object? sender, EventArgs e) { - await Shell.Current.GoToAsync(nameof(MyThirdPage)); - } - - ~MyPage() - { - Debug.WriteLine("~MyPage() called"); + await Shell.Current.GoToAsync(AppShell.GetRoute()); } } \ No newline at end of file diff --git a/CommunityMauiMediaElement/Views/MyThirdPage.xaml b/CommunityMauiMediaElement/Views/MyThirdPage.xaml index 2d2b4b7..555c366 100644 --- a/CommunityMauiMediaElement/Views/MyThirdPage.xaml +++ b/CommunityMauiMediaElement/Views/MyThirdPage.xaml @@ -8,6 +8,8 @@ Spacing="30">