Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Sample to Trigger MyPage Disposal #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CommunityMauiMediaElement/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
{
public partial class App : Application
{
public App()
public App(AppShell appShell)
{
InitializeComponent();

MainPage = new AppShell();
MainPage = appShell;
}
}
}
5 changes: 0 additions & 5 deletions CommunityMauiMediaElement/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,4 @@
Shell.FlyoutBehavior="Disabled"
Title="CommunityMauiMediaElement">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
45 changes: 34 additions & 11 deletions CommunityMauiMediaElement/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MainPage>(), typeof(MainPage));
Routing.RegisterRoute(GetRoute<MyPage>(), typeof(MyPage));
Routing.RegisterRoute(GetRoute<MyThirdPage>(), typeof(MyThirdPage));
}

public static string GetRoute<T>() 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();
}
}
}
49 changes: 28 additions & 21 deletions CommunityMauiMediaElement/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyThirdPage>());
}
}
}
4 changes: 4 additions & 0 deletions CommunityMauiMediaElement/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public static MauiApp CreateMauiApp()
builder.Logging.AddDebug();
#endif

builder.Services.AddSingleton<AppShell>();
builder.Services.AddSingleton<IFileSystem>(FileSystem.Current);

builder.Services.AddTransient<MainPage>();
builder.Services.AddTransient<MyPage>();
builder.Services.AddTransient<MyViewModel>();

Expand Down
18 changes: 2 additions & 16 deletions CommunityMauiMediaElement/ViewModels/MyViewModel.cs
Original file line number Diff line number Diff line change
@@ -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");
}
39 changes: 20 additions & 19 deletions CommunityMauiMediaElement/Views/MyPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyThirdPage>());
}
}
2 changes: 2 additions & 0 deletions CommunityMauiMediaElement/Views/MyThirdPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
Spacing="30">

<Label Text="Page after MediaElement > navigate back..." />
<Button Text="2) Go to next page > Unloaded gets called"
Clicked="Button_OnClicked" />

</VerticalStackLayout>
</ContentPage>
20 changes: 20 additions & 0 deletions CommunityMauiMediaElement/Views/MyThirdPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
using System.Diagnostics;
namespace CommunityMauiMediaElement.Views;

public partial class MyThirdPage : ContentPage
{
public MyThirdPage()
{
InitializeComponent();
Trace.WriteLine($"{this.GetType().FullName} Initialized");
}

protected override void OnAppearing()
{
base.OnAppearing();

GC.Collect();
GC.WaitForPendingFinalizers();
}

~MyThirdPage()
{
Trace.WriteLine($"{this.GetType().FullName} Disposed");
}

private async void Button_OnClicked(object? sender, EventArgs e)
{
await Shell.Current.GoToAsync(AppShell.GetRoute<MyPage>());
}
}