using AIK.Common.Language; using AIK.ViewModels; using AIK.Views.MenuPages.CommonPage; using AIK.Views.Utils; using CommunityToolkit.Mvvm.DependencyInjection; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace AIK.Views.AccountPages { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { private Storyboard _slideIn; private Storyboard _slideOut; public MainWindow() { InitializeComponent(); this.MaxHeight = SystemParameters.PrimaryScreenHeight;//防止最大化时系统任务栏被遮盖 DataContext = Ioc.Default.GetService(); // 加载动画 _slideIn = (Storyboard)FindResource("SlideInStoryboard"); _slideOut = (Storyboard)FindResource("SlideOutStoryboard"); // Esc 关闭面板 this.KeyDown += (s, e) => { if (e.Key == Key.Escape && UserPanelContainer.Visibility == Visibility.Visible) { _slideOut.Begin(); e.Handled = true; } }; } private void UserAvatarButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (UserPanelContainer.Visibility == Visibility.Visible) _slideOut.Begin(); else _slideIn.Begin(); e.Handled = true; } private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // 如果面板未显示,直接返回 if (UserPanelContainer.Visibility != Visibility.Visible) return; // 获取点击的原始元素 var clickedElement = e.OriginalSource as DependencyObject; // 检查是否点击在 UserPanelContainer 或其子元素上 if (IsAncestorOrSelf(UserPanelContainer, clickedElement)) { // 点击在面板内部 → 不关闭 return; } // 点击在外部 → 关闭面板 _slideOut.Begin(); e.Handled = true; } private bool IsAncestorOrSelf(DependencyObject target, DependencyObject element) { try { while (element != null) { if (element == target) return true; element = VisualTreeHelper.GetParent(element); } return false; } catch (Exception ex) { //忽略异常,防止因视觉树问题导致崩溃 return false; } } protected override void OnClosed(EventArgs e) { // 清理数据上下文 this.DataContext = null; base.OnClosed(e); } private void Button_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); } private void BtnMax_Click(object sender, RoutedEventArgs e) { this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized; UpdateMaximizeButton(); } // 更新最大化按钮图标 private void UpdateMaximizeButton() { if (BtnMax != null) { BtnMax.Content = this.WindowState switch { WindowState.Maximized => "\ue653", // 还原图标 _ => "\ue651" // 最大化图标 }; } } private void BtnMin_Click(object sender, RoutedEventArgs e) { this.WindowState = WindowState.Minimized; } // 内存不足时的处理 private void Application_Startup(object sender, StartupEventArgs e) { AppDomain.CurrentDomain.UnhandledException += (s, args) => { if (IsOutOfMemoryException(args.ExceptionObject as Exception)) { // 紧急内存清理 EmergencyMemoryCleanup(); MessageBox.Show("内存不足,已尝试清理。请保存工作并重启应用。"); } }; } #region 按钮点击 private void WeChatArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { string wechat = (this.FindName("TxtWeChat") as TextBlock)?.Text; if (!string.IsNullOrEmpty(wechat)) { _ = SafeClipboard.SetTextAsync(wechat); //ShowCopyTip("微信号已复制"); LightTipWindow.ShowTip(this, AiKLanguage.LanguageKey.GetValueOrDefault("ContactWechatCopy")); } } private void EmailArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { string email = (this.FindName("TxtEmail") as TextBlock)?.Text; if (!string.IsNullOrEmpty(email)) { _ = SafeClipboard.SetTextAsync(email); //ShowCopyTip("微信号已复制"); LightTipWindow.ShowTip(this, AiKLanguage.LanguageKey.GetValueOrDefault("ContactEmailCopy")); } } // 账密登录入口:在应用中心弹出账密登录窗口 private void AccountLoginLink_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { var loginWindow = new AccountLoginWindow { Owner = this }; loginWindow.ShowDialog(); e.Handled = true; } #endregion private bool IsOutOfMemoryException(Exception ex) { return ex is OutOfMemoryException || (ex?.InnerException is OutOfMemoryException); } private void EmergencyMemoryCleanup() { // 立即强制GC GC.Collect(2, GCCollectionMode.Forced, true); GC.WaitForPendingFinalizers(); GC.Collect(); } } }