Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
Merged
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 Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<IsWpfProject Condition="'$(IsDesignProject)' != 'true'">$(MSBuildProjectName.Contains('Wpf'))</IsWpfProject>
<IsFormsProject Condition="'$(IsDesignProject)' != 'true'">$(MSBuildProjectName.Contains('Forms'))</IsFormsProject>
<IsSampleProject>$(MSBuildProjectName.Contains('Sample'))</IsSampleProject>
<DefaultTargetPlatformVersion>18351</DefaultTargetPlatformVersion>
<DefaultTargetPlatformVersion>18362</DefaultTargetPlatformVersion>
<DefaultTargetPlatformMinVersion>18226</DefaultTargetPlatformMinVersion>
<PackageOutputPath>$(MSBuildThisFileDirectory)bin\nupkg</PackageOutputPath>
</PropertyGroup>
Expand All @@ -45,7 +45,7 @@
This also needs to be installed on your local machine. Can do this with PowerShell:
./build/Install-WindowsSDKISO.ps1 18351
-->
<TargetPlatformVersion>10.0.18351.0</TargetPlatformVersion>
<TargetPlatformVersion>10.0.18362.0</TargetPlatformVersion>
<!-- XAML Islands require SDK 18226 -->
<TargetPlatformMinVersion>10.0.18226.0</TargetPlatformMinVersion>

Expand Down
10 changes: 10 additions & 0 deletions Microsoft.Toolkit.Forms.UI.XamlHost/WindowsXamlHostBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,15 @@ protected override void OnHandleCreated(EventArgs e)
UpdateDpiScalingFactor();
}
}

/// <summary>
/// Override that disposes the current instance when the parent handle has been destroyed
/// </summary>
/// <param name="e">Ignored</param>
protected override void OnHandleDestroyed(EventArgs e)
{
this.Dispose(true);
base.OnHandleDestroyed(e);
}
}
}
24 changes: 24 additions & 0 deletions Microsoft.Toolkit.Wpf.UI.XamlHost/WindowsXamlHostBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public object GetUwpInternalObject()
/// </summary>
public bool IsDisposed { get; private set; }

private System.Windows.Window _parentWindow;

/// <summary>
/// Creates <see cref="WUX.Application" /> object, wrapped <see cref="WUX.Hosting.DesktopWindowXamlSource" /> instance; creates and
/// sets root UWP XAML element on <see cref="WUX.Hosting.DesktopWindowXamlSource" />.
Expand All @@ -200,6 +202,12 @@ public object GetUwpInternalObject()
/// <returns>Handle to XAML window</returns>
protected override HandleRef BuildWindowCore(HandleRef hwndParent)
{
this._parentWindow = System.Windows.Window.GetWindow(this);
if (_parentWindow != null)
{
_parentWindow.Closed += this.OnParentClosed;
}

ComponentDispatcher.ThreadFilterMessage += this.OnThreadFilterMessage;

// 'EnableMouseInPointer' is called by the WindowsXamlManager during initialization. No need
Expand Down Expand Up @@ -230,6 +238,16 @@ protected virtual void SetContent()
}
}

/// <summary>
/// Disposes the current instance in response to the parent window getting destroyed.
/// </summary>
/// <param name="sender">Paramter sender is ignored</param>
/// <param name="e">Parameter args is ignored</param>
private void OnParentClosed(object sender, EventArgs e)
{
this.Dispose(true);
}

/// <summary>
/// WPF framework request to destroy control window. Cleans up the HwndIslandSite created by DesktopWindowXamlSource
/// </summary>
Expand Down Expand Up @@ -260,6 +278,12 @@ protected override void Dispose(bool disposing)
{
_xamlSource.TakeFocusRequested -= OnTakeFocusRequested;
}

if (_parentWindow != null)
{
_parentWindow.Closed -= this.OnParentClosed;
_parentWindow = null;
}
}

// Free any unmanaged objects here.
Expand Down