Files
EBoard/电子展板/ViewModels/MainWindowViewModel.cs
2025-09-23 10:38:35 +08:00

53 lines
1.8 KiB
C#

using Nancy;
using Nancy.Hosting.Self;
using Nancy.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using .Base;
namespace .ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
private Window window;
public DelegateCommand<Window> LoadedCommand { get; set; }
public DelegateCommand<CancelEventArgs> WindowClosingCommand { get; set; }
public MainWindowViewModel()
{
LoadedCommand = new DelegateCommand<Window>(Loaded);
WindowClosingCommand = new DelegateCommand<CancelEventArgs>(WindowClosing);
}
private void Loaded(Window window)
{
this.window = window;
int screenIndex = 0;
this.window.Left = (int)(Screen.AllScreens[screenIndex].Bounds.Left / GetDpiScaleX());
this.window.Top = (int)(Screen.AllScreens[screenIndex].Bounds.Top / GetDpiScaleX());
this.window.Width = (int)(Screen.AllScreens[screenIndex].Bounds.Width / GetDpiScaleX());
this.window.Height = (int)(Screen.AllScreens[screenIndex].Bounds.Height / GetDpiScaleX());
GlobalVariable.WebServer = new NancyHost(new Uri("http://localhost:80"));
GlobalVariable.WebServer.Start();
}
private void WindowClosing(CancelEventArgs args)
{
GlobalVariable.WebServer?.Stop();
GlobalVariable.WebServer?.Dispose();
GlobalVariable.WebServer = null;
Environment.Exit(0);
}
public double GetDpiScaleX()
{
return VisualTreeHelper.GetDpi(this.window).DpiScaleX;
}
}
}