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

[Windows] Disconnect events and dispose the Content before close the Window #10886

Closed
wants to merge 2 commits 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.Maui.Platform;
using Microsoft.UI.Xaml;

Expand All @@ -19,6 +20,8 @@ public static void MapCloseWindow(ApplicationHandler handler, IApplication appli
{
if (args is IWindow window)
{
(window.Handler as WindowHandler)?.Dispose();

(window.Handler?.PlatformView as Window)?.Close();
}
}
Expand Down
22 changes: 17 additions & 5 deletions src/Core/src/Handlers/Window/WindowHandler.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Microsoft.Maui.Graphics;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Maui.ApplicationModel;

Expand Down Expand Up @@ -32,22 +31,35 @@ protected override void ConnectHandler(UI.Xaml.Window platformView)
}

protected override void DisconnectHandler(UI.Xaml.Window platformView)
{
Dispose();

base.DisconnectHandler(platformView);
}

internal void Dispose()
{
MauiContext
?.GetNavigationRootManager()
?.Disconnect();

if (platformView.Content is WindowRootViewContainer container)
if (PlatformView?.Content is WindowRootViewContainer container)
{
container.Children.Clear();
platformView.Content = null;
VirtualView?.Content?.Handler?.DisconnectHandler();
PlatformView.Content = null;
}

var appWindow = platformView.GetAppWindow();
var appWindow = PlatformView?.GetAppWindow();

if (appWindow is not null)
{
appWindow.Changed -= OnWindowChanged;
appWindow.Destroy();
}

base.DisconnectHandler(platformView);
if (PlatformView is MauiWinUIWindow mauiWinUIWindow)
mauiWinUIWindow.Dispose();
}

public static void MapTitle(IWindowHandler handler, IWindow window) =>
Expand Down
12 changes: 11 additions & 1 deletion src/Core/src/Platform/Windows/MauiWinUIWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Microsoft.Maui
{
public class MauiWinUIWindow : UI.Xaml.Window, IPlatformSizeRestrictedWindow
public class MauiWinUIWindow : UI.Xaml.Window, IPlatformSizeRestrictedWindow, IDisposable
{
static readonly SizeInt32 DefaultMinimumSize = new SizeInt32(0, 0);
static readonly SizeInt32 DefaultMaximumSize = new SizeInt32(int.MaxValue, int.MaxValue);
Expand Down Expand Up @@ -178,6 +178,16 @@ internal void UpdateTitleOnCustomTitleBar()

[DllImport("user32.dll", SetLastError = true)]
static extern int DestroyIcon(IntPtr hIcon);

public void Dispose()
{
Activated -= OnActivated;
Closed -= OnClosedPrivate;
VisibilityChanged -= OnVisibilityChanged;

if (_windowManager != null)
_windowManager.Dispose();
}
}

interface IPlatformSizeRestrictedWindow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Microsoft.Maui.IMenuFlyoutSeparator
Microsoft.Maui.IToolTipElement
Microsoft.Maui.IToolTipElement.ToolTip.get -> Microsoft.Maui.ToolTip?
Microsoft.Maui.IWindow.FrameChanged(Microsoft.Maui.Graphics.Rect frame) -> void
Microsoft.Maui.MauiWinUIWindow.Dispose() -> void
Microsoft.Maui.ToolTip
Microsoft.Maui.ToolTip.Content.get -> object?
Microsoft.Maui.ToolTip.Content.set -> void
Expand Down