fix: memory leak of some controls. (#1482)

This commit is contained in:
NaBian 2024-07-13 14:56:37 +08:00
parent a447ac3e75
commit 9092fe6ec7
4 changed files with 40 additions and 3 deletions

View File

@ -8,7 +8,7 @@
xmlns:hc="https://handyorg.github.io/handycontrol">
<hc:TransitioningContentControl>
<StackPanel Margin="32">
<hc:ImageViewer Background="{DynamicResource SecondaryRegionBrush}" MaxWidth="600" Height="330" ImageSource="/HandyControlDemo;component/Resources/Img/1.jpg"/>
<hc:ImageViewer x:Name="ImageViewerDemo" Background="{DynamicResource SecondaryRegionBrush}" MaxWidth="600" Height="330" ImageSource="/HandyControlDemo;component/Resources/Img/1.jpg"/>
<Button Margin="0,32,0,0" Command="{Binding OpenImgCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.OpenImageBrowser}}" Style="{StaticResource ButtonPrimary}" HorizontalAlignment="Center"/>
</StackPanel>
</hc:TransitioningContentControl>

View File

@ -1,10 +1,17 @@

using System;
namespace HandyControlDemo.UserControl;
public partial class ImageBrowserDemoCtl
public partial class ImageBrowserDemoCtl : IDisposable
{
public ImageBrowserDemoCtl()
{
InitializeComponent();
}
public void Dispose()
{
ImageViewerDemo.Dispose();
}
}

View File

@ -23,7 +23,7 @@ namespace HandyControl.Controls;
[TemplatePart(Name = ElementBorderMove, Type = typeof(Border))]
[TemplatePart(Name = ElementBorderBottom, Type = typeof(Border))]
[TemplatePart(Name = ElementImageMain, Type = typeof(Image))]
public class ImageViewer : Control
public class ImageViewer : Control, IDisposable
{
#region Constants
@ -167,6 +167,8 @@ public class ImageViewer : Control
private ImageBrowser _imageBrowser;
private bool _isDisposed;
#endregion Data
#region ctor
@ -989,4 +991,25 @@ public class ImageViewer : Control
}
}
}
protected virtual void Dispose(bool disposing)
{
if (!_isDisposed)
{
if (disposing)
{
ImageSource = null;
_imageMain.Source = null;
_imageMain.UpdateLayout();
}
_isDisposed = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
@ -110,6 +111,12 @@ public class ImageBrowser : Window
}
}
protected override void OnClosing(CancelEventArgs e)
{
_imageViewer?.Dispose();
base.OnClosing(e);
}
private void ButtonClose_OnClick(object sender, RoutedEventArgs e) => Close();
private void PanelTopOnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)