Files
EBoard/电子展板/ViewModels/MainWindowViewModel.cs
2025-09-26 17:42:33 +08:00

107 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using .Base;
using .Models;
using .Utility;
using .Utility.Core;
using .Utility.Extension;
namespace .ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
private Window window;
public ImageSource Image1 { get; set; }
public ImageSource Image2 { get; set; }
public ImageSource Image3 { get; set; }
public ImageSource Image4 { get; set; }
public DelegateCommand<Window> LoadedCommand { get; set; }
public DelegateCommand<CancelEventArgs> WindowClosingCommand { get; set; }
public MainWindowViewModel()
{
LoadedCommand = new DelegateCommand<Window>(Loaded);
WindowClosingCommand = new DelegateCommand<CancelEventArgs>(WindowClosing);
EventBus.Instance.Subscribe(this, "save", ReloadImage);
InitImage();
}
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());
WebServer.Start();
//GlobalVariable.WebServer = new NancyHost(new Uri("http://localhost:80"));
//GlobalVariable.WebServer.Start();
}
private void WindowClosing(CancelEventArgs args)
{
WebServer.Stop();
//GlobalVariable.WebServer?.Stop();
//GlobalVariable.WebServer?.Dispose();
//GlobalVariable.WebServer = null;
Environment.Exit(0);
}
public double GetDpiScaleX()
{
return VisualTreeHelper.GetDpi(this.window).DpiScaleX;
}
private void ReloadImage(string obj)
{
App.Current.Dispatcher.Invoke(() =>
{
InitImage();
});
}
private void InitImage()
{
Image1 = GetImage(Config.CPCMember1Path);
Image2 = GetImage(Config.CPCMember2Path);
Image3 = GetImage(Config.CPCMember3Path);
Image4 = GetImage(Config.CPCMember4Path);
}
private ImageSource GetImage(string path)
{
try
{
if (path.IsNullOrEmpty())
return null;
UploadFiles uploadFiles = GlobalVariable.Config.FileUploadList.FirstOrDefault(x => path.EndsWith(x.Name));
if (uploadFiles == null)
return null;
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = new ByteArrayStream(uploadFiles.Bin);
image.EndInit();
image.Freeze();
return image;
}
catch (Exception ex)
{
return null;
}
}
}
}