using Aitex.Core.RT.Key; using Aitex.Core.RT.Log; using Aitex.Core.Util; using System; using System.Collections.Generic; using System.IO; 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.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace MECF.Framework.RT.Core.Backend { /// /// LicenseView.xaml 的交互逻辑 /// public partial class LicenseView : UserControl { PeriodicJob _thread; public LicenseView() { InitializeComponent(); _thread = new PeriodicJob(500, OnTimer, "LicenseView Update", false, true); IsVisibleChanged += LicenseView_IsVisibleChanged; } private void LicenseView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { if (IsVisible) _thread.Start(); else _thread.Pause(); } bool OnTimer() { try { Application.Current.Dispatcher.Invoke(new Action(() => { MachineCode.Text = KeyManager.Instance.LocalMachineCode; ExpireDateTime.Text = KeyManager.Instance.ExpireDateTime.ToString(); LeftDays.Text = KeyManager.Instance.LeftDays.ToString(); LastRegisterDateTime.Text = KeyManager.Instance.LastRegisterDateTime.ToString(); LastRegisterDays.Text = KeyManager.Instance.LastRegisterDays.ToString(); CurrentDateTime.Text = KeyManager.Instance.CurrentDateTime.ToString(); })); } catch (Exception ex) { LOG.Write(ex); } return true; } private void Copy_Click(object sender, RoutedEventArgs e) { string key = MachineCode.Text.Trim(); if (!string.IsNullOrEmpty(key)) { Clipboard.SetDataObject(key); System.Windows.MessageBox.Show("机器码已经复制到剪贴板", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } } private void Export_Click(object sender, RoutedEventArgs e) { string key = MachineCode.Text.Trim(); if (string.IsNullOrEmpty(key)) return; System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog(); sfd.DefaultExt = ".code"; sfd.Filter = ".code|*.*"; sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); sfd.FileName = string.Format("jet_machine_code_{0}.code", DateTime.Now.ToString("yyyyMMdd_HHmmssfff")); if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return; try { byte[] myByte = System.Text.Encoding.UTF8.GetBytes(key); using (FileStream fsWrite = new FileStream(sfd.FileName, FileMode.OpenOrCreate)) { fsWrite.Write(myByte, 0, myByte.Length); } System.Windows.MessageBox.Show(string.Format("机器码导出到文件\r\n{0}\r\n成功", sfd.FileName), "提示", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Warning); } } private void Paste_Click(object sender, RoutedEventArgs e) { if (!Clipboard.ContainsText()) return; Key.Text = Clipboard.GetText(); } private void Import_Click(object sender, RoutedEventArgs e) { System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog(); ofd.DefaultExt = ".code"; ofd.Filter = ".code|*.*"; ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); //ofd.FileName = string.Format("machine_code_{0}.code", DateTime.Now.ToString("yyyyMMdd_HHmmssfff")); if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return; try { using (FileStream fsSource = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read)) { // Read the source file into a byte array. byte[] bytes = new byte[fsSource.Length]; fsSource.Read(bytes, 0, (int)fsSource.Length); Key.Text = System.Text.Encoding.UTF8.GetString(bytes); } System.Windows.MessageBox.Show("导入注册码成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Warning); } } private void Register_Click(object sender, RoutedEventArgs e) { string inputKey = Key.Text.Trim(); if (string.IsNullOrEmpty(inputKey)) return; string reason; if (!KeyManager.Instance.Register(inputKey, out reason)) { System.Windows.MessageBox.Show(reason, "提示", MessageBoxButton.OK, MessageBoxImage.Warning); return; } System.Windows.MessageBox.Show(reason, "提示", MessageBoxButton.OK, MessageBoxImage.Information); } } }