Пример #1
0
        // Khi click vào nút tìm kiếm

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Show();
            var config = ConfigurationManager.AppSettings["ShowSplash"];

            if (config.ToLower() == "true")
            {
                var screen = new Views.SplashWindow();
                screen.ShowDialog();
            }
            CurrentViewModel = GridMain.DataContext as MainViewModel;
        }
Пример #2
0
        public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;

            #region 属性初始化

            Window mainWindow = Application.Current.MainWindow;

            WinState = WindowState.Normal;

            MinimizeIcon = SystemIcon.Instance().Minimize;
            ResizeIcon   = SystemIcon.Instance().Maximize;
            CloseIcon    = SystemIcon.Instance().Close;
            SkinIcon     = SystemIcon.Instance().Skin;

            #endregion

            #region 订阅

            // 订阅导航事件
            eventAggregator.GetEvent <NavigationEvent>().Subscribe((view) =>
            {
                var param = new NavigationParameters
                {
                    { "Parent", view.ParentViewName },
                    { "Parameter", view.Parameter }
                };
                regionManager.RequestNavigate("ContentRegion", view.ViewName, param);
            });

            // 订阅消息发送事件
            string oldMessage;
            eventAggregator.GetEvent <MessageEvent>().Subscribe((message) =>
            {
                MessageVisibility = Visibility.Visible;

                oldMessage = Message;
                Message    = message;
                int sleep  = 2000;
                if (oldMessage == Message)
                {
                    sleep = 1500;
                }

                Thread.Sleep(sleep);

                MessageVisibility = Visibility.Hidden;
            }, ThreadOption.BackgroundThread);

            #endregion

            #region 命令定义

            // window加载后执行的事件
            LoadedCommand = new DelegateCommand(() =>
            {
                clipboardHooker = new ClipboardHooker(Application.Current.MainWindow);
                clipboardHooker.ClipboardUpdated += OnClipboardUpdated;

                // 进入首页
                var param = new NavigationParameters
                {
                    { "Parent", "" },
                    { "Parameter", "start" }
                };
                regionManager.RequestNavigate("ContentRegion", ViewIndexViewModel.Tag, param);

                // 关闭欢迎页
                if (App.Dictionary.ContainsKey("SplashWindow"))
                {
                    Views.SplashWindow sw = App.Dictionary["SplashWindow"] as Views.SplashWindow;
                    // 在sw的线程上关闭SplashWindow
                    sw.Dispatcher.Invoke(() => sw.Close());
                }

                // 设置焦点
                Application.Current.MainWindow.Activate();
            });

            // 顶部caption栏的点击事件,包括双击和拖动
            int times = 0;
            DragMoveCommand = new DelegateCommand(() =>
            {
                // caption 双击事件
                times += 1;
                DispatcherTimer timer = new DispatcherTimer
                {
                    Interval = new TimeSpan(0, 0, 0, 0, 300)
                };
                timer.Tick     += (s, e) => { timer.IsEnabled = false; times = 0; };
                timer.IsEnabled = true;

                if (times % 2 == 0)
                {
                    timer.IsEnabled = false;
                    times           = 0;
                    WinState        = WinState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
                }

                // caption 拖动事件
                try
                {
                    mainWindow.DragMove();
                }
                catch { }
            });

            // 最小化窗口事件
            MinimizeCommand = new DelegateCommand(() =>
            {
                mainWindow.WindowState = WindowState.Minimized;
            });
            MinimizeEnterCommand = new DelegateCommand(() =>
            {
                SetEnterStyle(MinimizeIcon);
            });
            MinimizeLeaveCommand = new DelegateCommand(() =>
            {
                SetLeaveStyle(MinimizeIcon);
            });

            // 最大化/还原窗口事件
            ResizeCommand = new DelegateCommand(() =>
            {
                WinState = WinState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
            });
            ResizeEnterCommand = new DelegateCommand(() =>
            {
                SetEnterStyle(ResizeIcon);
            });
            ResizeLeaveCommand = new DelegateCommand(() =>
            {
                SetLeaveStyle(ResizeIcon);
            });

            // 关闭窗口事件
            CloseCommand = new DelegateCommand(() =>
            {
                if (clipboardHooker != null)
                {
                    clipboardHooker.ClipboardUpdated -= OnClipboardUpdated;
                    clipboardHooker.Dispose();
                }

                mainWindow.Close();
            });
            CloseEnterCommand = new DelegateCommand(() =>
            {
                SetEnterStyle(CloseIcon);
            });
            CloseLeaveCommand = new DelegateCommand(() =>
            {
                SetLeaveStyle(CloseIcon);
            });

            // 皮肤按钮点击事件
            SkinCommand = new DelegateCommand(() =>
            {
                // 设置主题
                DictionaryResource.LoadTheme("ThemeDiy");

                // 切换语言
                DictionaryResource.LoadLanguage("en_US");
            });
            SkinEnterCommand = new DelegateCommand(() =>
            {
                SetEnterStyle(SkinIcon);
            });
            SkinLeaveCommand = new DelegateCommand(() =>
            {
                SetLeaveStyle(SkinIcon);
            });

            #endregion
        }