Workaround a bug where WindowsXamlHostBase needs to be Disposed when the parent Window is closed.

This commit is contained in:
Oscar Calvo 2019-04-09 13:41:08 -07:00
Родитель 855a89adb5
Коммит 3ef66b6f15
3 изменённых файлов: 36 добавлений и 2 удалений

Просмотреть файл

@ -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>
@ -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>

Просмотреть файл

@ -280,5 +280,15 @@ namespace Microsoft.Toolkit.Forms.UI.XamlHost
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();
base.OnHandleDestroyed(e);
}
}
}

Просмотреть файл

@ -192,6 +192,8 @@ namespace Microsoft.Toolkit.Wpf.UI.XamlHost
/// </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" />.
@ -200,6 +202,12 @@ namespace Microsoft.Toolkit.Wpf.UI.XamlHost
/// <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
@ -230,6 +238,16 @@ namespace Microsoft.Toolkit.Wpf.UI.XamlHost
}
}
/// <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();
}
/// <summary>
/// WPF framework request to destroy control window. Cleans up the HwndIslandSite created by DesktopWindowXamlSource
/// </summary>
@ -260,6 +278,12 @@ namespace Microsoft.Toolkit.Wpf.UI.XamlHost
{
_xamlSource.TakeFocusRequested -= OnTakeFocusRequested;
}
if (_parentWindow != null)
{
_parentWindow.Closed -= this.OnParentClosed;
_parentWindow = null;
}
}
// Free any unmanaged objects here.