using System.ComponentModel; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Animation; using HandyControl.Controls; using HandyControl.Tools; using log4net; using log4net.Config; using SparkClient.Model.Helper; using SparkClient.ViewModel; using SparkClient.ViewModel.BaseWindow; using SparkClient.Views.BaseWindow; using MessageBox = SparkClient.Views.Dialog.MessageBox; using Window = System.Windows.Window; namespace SparkClient; /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow { private static readonly ILog Logger = LogManager.GetLogger(typeof(MainWindow)); public MainWindow() { InitializeComponent(); Logger.Info("Application started"); var vm = DataContext as MainWindowViewModel; if (vm != null) { vm.RequestMinimizeWindow += () => { this.WindowState = WindowState.Minimized; }; } // 强制设置应用窗口大小为 1920x1080 this.Width = 1920; this.Height = 1080; } public static extern int GetDpiForSystem(); protected override void OnContentRendered(EventArgs e) { base.OnContentRendered(e); MainViewModel mainVM = new MainViewModel(); DataContext = mainVM; WindowManager.mainViewModel = mainVM; Logger.InfoFormat("窗口加载:"+mainVM.GetType()); } private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) { //初始化 // ConfigHelper.Instance.SetLang("en"); DataBaseHelper.CreateConnection(); this.WindowState = WindowState.Maximized; } #region 重写窗体操作按钮 private void Border_Minimize_MouseEnter(object sender, MouseEventArgs e) { // 鼠标进入时更改背景色 if (sender is Border border) { border.Background = new SolidColorBrush(Color.FromArgb(50, 255, 255, 255)); } } private void Border_Close_MouseEnter(object sender, MouseEventArgs e) { // 鼠标进入时更改背景色 if (sender is Border border) { border.Background = new SolidColorBrush(Color.FromArgb(50, 255, 0, 0)); } } private void Border_MouseLeave(object sender, MouseEventArgs e) { // 鼠标离开时恢复背景色 if (sender is Border border) { border.Background = new SolidColorBrush(Colors.Transparent); } } private void Minimize_Click(object sender, MouseButtonEventArgs e) { this.WindowState = WindowState.Minimized; } private void Close_Click(object sender, MouseButtonEventArgs e) { var message = MultilingualHelper.getString("ExitAsk"); var title = MultilingualHelper.getString("ExitAskTitle"); MessageBoxResult result = HandyControl.Controls.MessageBox.Show( message, title, MessageBoxButton.YesNo, MessageBoxImage.Question ); if (result == MessageBoxResult.Yes) { DataBaseHelper.CloseConnection(); this.Close(); Environment.Exit(0); } } private void UIElement_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { try { this.DragMove(); } catch (InvalidOperationException ex) { } } } #endregion private void MainWindow_OnStateChanged(object? sender, EventArgs e) { if (this.WindowState == WindowState.Normal || this.WindowState == WindowState.Maximized) { this.WindowState = WindowState.Maximized; } } private void FrameworkElement_OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { } private void MainWindow_OnClosing(object? sender, CancelEventArgs e) { var message = MultilingualHelper.getString("ExitAsk"); var title = MultilingualHelper.getString("ExitAskTitle"); // MessageBoxResult result = HandyControl.Controls.MessageBox.Show( message, title, MessageBoxButton.YesNo, MessageBoxImage.Question ); MessageBoxResult result = new MessageBox().ShowAsk(message); if (result == MessageBoxResult.OK) { DataBaseHelper.CloseConnection(); // this.Close(); Environment.Exit(0); } e.Cancel = true; } private object _lastContent; private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e) { var cc = (ContentControl)sender; // 可以监听 Content 属性改变:Binding 上加 NotifyOnTargetUpdated="True" 后,响应 TargetUpdated // 或者监听 DataContextChanged,但要注意真正变的是 cc.Content 而不是 DataContext // 这里演示给 ContentControl 加一个依赖属性监听 cc.SizeChanged += (s, args) => { /* 有需要时也可在此动画 */ }; // 也可以用 Binding 的 SourceUpdated/TargetUpdated,这里简单写一个例子 —— 每次 LayoutUpdated 检查是否变更 cc.LayoutUpdated += (s, args) => { // 如果内容发生了变化,则执行淡入动画 if (cc.Content != _lastContent) { _lastContent = cc.Content; // 如果新内容不为空,就做一个 0→1 的淡入 if (cc.Content != null) { var fade = new DoubleAnimation { From = 0.0, To = 1.0, Duration = TimeSpan.FromSeconds(0.5), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut } }; cc.BeginAnimation(UIElement.OpacityProperty, fade); } } }; } }