mirror of
https://github.com/LanZhan-Harmony/WindowsMusicPlayer-TheUntamedMusicPlayer.git
synced 2026-05-06 19:20:18 +08:00
修复内存泄漏
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
xmlns:local="using:UntamedMusicPlayer.Controls"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:ui="using:CommunityToolkit.WinUI"
|
||||
Closed="Window_Closed"
|
||||
mc:Ignorable="d">
|
||||
|
||||
<Window.SystemBackdrop>
|
||||
|
||||
@@ -20,10 +20,14 @@ public sealed partial class ImageViewerWindow : Window, IDisposable
|
||||
{
|
||||
private readonly ILogger _logger = LoggingService.CreateLogger<ImageViewerWindow>();
|
||||
|
||||
private readonly Guid _windowId;
|
||||
private readonly BitmapImage _image;
|
||||
private readonly IDetailedSongInfoBase _info;
|
||||
|
||||
public ImageViewerWindow(IDetailedSongInfoBase info)
|
||||
private bool _closed = false;
|
||||
private bool _isDisposed = false;
|
||||
|
||||
public ImageViewerWindow(Guid windowId, IDetailedSongInfoBase info)
|
||||
{
|
||||
InitializeComponent();
|
||||
AppWindow.SetTaskbarIcon(Path.Combine(AppContext.BaseDirectory, "Assets/AppIcon/Icon.ico"));
|
||||
@@ -40,6 +44,7 @@ public sealed partial class ImageViewerWindow : Window, IDisposable
|
||||
((FrameworkElement)Content).RequestedTheme
|
||||
);
|
||||
|
||||
_windowId = windowId;
|
||||
_image = info.Cover!;
|
||||
_info = info;
|
||||
|
||||
@@ -200,8 +205,26 @@ public sealed partial class ImageViewerWindow : Window, IDisposable
|
||||
return ".jpeg";
|
||||
}
|
||||
|
||||
private void Window_Closed(object sender, WindowEventArgs args)
|
||||
{
|
||||
_closed = true;
|
||||
Dispose();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Close();
|
||||
if (_isDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isDisposed = true;
|
||||
|
||||
if (!_closed)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
Data.ImageViewerWindows?.Remove(_windowId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -424,7 +424,13 @@ public sealed partial class MainWindow : WindowEx, IRecipient<LogMessage>
|
||||
sender.Hide(); // 立即隐藏窗口,提升视觉响应
|
||||
Data.MusicPlayer.Pause(); // 立即停止音乐播放
|
||||
Data.DesktopLyricWindow?.Dispose(); // 立即关闭桌面歌词
|
||||
Data.ImageViewerWindows?.ForEach(w => w.Dispose()); // 立即关闭图片查看器
|
||||
if (Data.ImageViewerWindows is not null) // 立即关闭图片查看器
|
||||
{
|
||||
foreach (var window in Data.ImageViewerWindows.Values)
|
||||
{
|
||||
window.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Settings.NotFirstUsed = true;
|
||||
// 并行执行保存以缩短退出后的存活时间
|
||||
|
||||
@@ -95,7 +95,7 @@ public static class Data
|
||||
public static LyricPage? LyricPage { get; set; }
|
||||
public static RootPlayBarView? RootPlayBarView { get; set; }
|
||||
public static DesktopLyricWindow? DesktopLyricWindow { get; set; }
|
||||
public static List<ImageViewerWindow>? ImageViewerWindows { get; set; }
|
||||
public static Dictionary<Guid, ImageViewerWindow>? ImageViewerWindows { get; set; }
|
||||
#endregion
|
||||
|
||||
#region ViewModels
|
||||
|
||||
@@ -131,7 +131,11 @@ public sealed partial class LyricViewModel : ObservableObject, IDisposable
|
||||
public async void ShowCoverButton_Click(object _1, RoutedEventArgs _2)
|
||||
{
|
||||
Data.ImageViewerWindows ??= [];
|
||||
Data.ImageViewerWindows.Add(new ImageViewerWindow(Data.PlayState.CurrentSong!));
|
||||
var windowId = Guid.CreateVersion7();
|
||||
Data.ImageViewerWindows.Add(
|
||||
windowId,
|
||||
new ImageViewerWindow(windowId, Data.PlayState.CurrentSong!)
|
||||
);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
Reference in New Issue
Block a user