Browse Source

1.优化设置窗口;
2.优化主页窗口;
3.完善程序逻辑。

Yumin 6 years ago
parent
commit
b0beb5cfee
4 changed files with 87 additions and 87 deletions
  1. 26 26
      FaceChange/AppContext.cs
  2. 4 4
      FaceChange/MainWindow.xaml
  3. 39 15
      FaceChange/MainWindow.xaml.cs
  4. 18 42
      FaceChange/SettingsWindow.xaml.cs

+ 26 - 26
FaceChange/AppContext.cs

@@ -12,19 +12,19 @@ namespace FaceChange
     class AppContext
     {
         // 串口类
-        SerialPort serialPort = null;
+        private SerialPort _serialPort = null;
         // 打开串口标志位
         public bool portOpen = false;
         // Data
-        private List<FaceInfo> faces = new List<FaceInfo>();
+        private List<FaceInfo> _faces = new List<FaceInfo>();
 
-        private static AppContext appContext;
+        private static AppContext _appContext;
 
         private AppContext() { }
 
         public static AppContext GetAppContext()
         {
-            return appContext ?? (appContext = new AppContext());
+            return _appContext ?? (_appContext = new AppContext());
         }
 
         public SerialPort SerialPort
@@ -45,11 +45,11 @@ namespace FaceChange
                     MainWindow.Status.ToolTip = "设备已连接";
                     MainWindow.Status.Foreground = new SolidColorBrush(Colors.Green);
                 }
-                serialPort = value;
+                _serialPort = value;
             }
             get
             {
-                return serialPort;
+                return _serialPort;
             }
         }
 
@@ -69,33 +69,33 @@ namespace FaceChange
         {
             set
             {
-                faces = value;
+                _faces = value;
             }
             get
             {
+                _faces.Clear();
                 // 读取配置
                 var prop = Properties.Settings.Default.Faces;
-                if (prop != null && !prop.Trim().Equals(""))
+                if (prop == null || prop.Trim().Equals("")) return _faces;
+                var faceStringList = prop.Split('|').ToList();
+                foreach (var faceString in faceStringList)
                 {
-                    var faceStringList = prop.Split('|').ToList();
-                    foreach (var faceString in faceStringList)
+                    var fileName = faceString.Substring(
+                        faceString.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
+                    try
                     {
-                        var fileName = faceString.Substring(
-                            faceString.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
-                        try
-                        {
-                            faces.Add(new FaceInfo(
-                                new BitmapImage(new Uri(faceString)),
-                                fileName,
-                                faceString.Replace(fileName, @"")));
-                        }
-                        catch (Exception exception)
-                        {
-                            Console.WriteLine(exception);
-                        }
+                        _faces.Add(new FaceInfo(
+                            new BitmapImage(new Uri(faceString)),
+                            fileName,
+                            faceString.Replace(fileName, @"")));
+                    }
+                    catch (Exception exception)
+                    {
+                        Console.WriteLine(exception);
                     }
                 }
-                return faces;
+                Console.WriteLine("123");
+                return _faces;
             }
         }
 
@@ -104,10 +104,10 @@ namespace FaceChange
             try
             {
                 // Comm.BytesToRead 中为要读入的字节长度
-                int len = serialPort.BytesToRead;
+                int len = _serialPort.BytesToRead;
                 Byte[] readBuffer = new Byte[len];
                 // 将数据读入缓存
-                serialPort.Read(readBuffer, 0, len);
+                _serialPort.Read(readBuffer, 0, len);
                 // 处理 readBuffer 中的数据,自定义处理过程
                 string msg = Encoding.Default.GetString(readBuffer, 0, len);
                 Console.WriteLine(msg);

+ 4 - 4
FaceChange/MainWindow.xaml

@@ -5,7 +5,7 @@
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:FaceChange"
         mc:Ignorable="d"
-        Title="魔幻变脸" Height="500" Width="500" KeyDown="Window_KeyDown" ResizeMode="CanMinimize" Loaded="Window_Loaded">
+        Title="魔幻变脸" Height="500" Width="500" KeyDown="Window_KeyDown" ResizeMode="CanMinimize" Loaded="Window_Loaded" Closing="Window_Closing">
     <Grid>
         <ToolBar x:Name="ToolBar" VerticalAlignment="Top" Padding="0,0" Height="32">
             <Button x:Name="Settings" Content="&#xF013; 设置" ToolTip="设置" Style="{StaticResource FontAwesome}" Click="Settings_Click" VerticalAlignment="Stretch"/>
@@ -13,9 +13,9 @@
             <Button x:Name="FullScreen" Content="&#xF31E; 全屏" ToolTip="全屏" Style="{StaticResource FontAwesome}" Click="FullScreen_Click" VerticalAlignment="Stretch"/>
             <Separator/>
             <Label x:Name="PortStatus" Content="未连接" ToolTip="设备连接" Foreground="Red"/>
+            <Separator/>
+            <Label x:Name="Name" Content="" Foreground="Gray"/>
         </ToolBar>
-        <Viewbox HorizontalAlignment="Center">
-            <Image x:Name="Face" Stretch="UniformToFill" />
-        </Viewbox>
+        <Image x:Name="Face" Margin="0,32,0,0"/>
     </Grid>
 </Window>

+ 39 - 15
FaceChange/MainWindow.xaml.cs

@@ -1,8 +1,6 @@
 using System.Windows;
 using System.Windows.Input;
-using System.Windows.Media;
 using System.Windows.Controls;
-using System.Windows.Media.Imaging;
 using System;
 using System.Collections.Generic;
 
@@ -19,7 +17,6 @@ namespace FaceChange
         private List<FaceInfo> faces;
 
         public static Label Status;
-        private DrawingImage imageSource;
 
         public MainWindow()
         {
@@ -35,28 +32,56 @@ namespace FaceChange
 
         private void Window_KeyDown(object sender, KeyEventArgs e)
         {
-            if (e.Key == System.Windows.Input.Key.Escape)
+            if (e.Key == Key.Escape)
             {
                 ToolBar.Visibility = Visibility.Visible;
-                Thickness thickness = new Thickness();
-                thickness.Top = 30;
-                //mediaElement.Margin = thickness;
+                Face.Margin = new Thickness {Left = 0, Top = 32, Right = 0, Bottom = 0};
                 this.WindowState = WindowState.Normal;
                 this.WindowStyle = WindowStyle.SingleBorderWindow;
             }
             if (e.Key == Key.Space)
             {
-                int index = new Random().Next(0, faces.Count);
-                Console.WriteLine(index);
-                Face.Source = faces[index].Face;
+                if (faces.Count == 0)
+                {
+                    MessageBox.Show("请先在设置页添加资源", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
+                }
+                else
+                {
+                    var index = GetNoRepeatRandom(0, faces.Count);
+                    Name.Content = faces[index].Name;
+                    Face.Source = faces[index].Face;
+                    Console.WriteLine(index);
+                }
             }
         }
+        
+        private int _lastIndex = -1;
+        private int GetNoRepeatRandom(int min, int max)
+        {
+            if (max - min <= 1)
+            {
+                return min;
+            }
+            var r = new Random();
+            var i = r.Next(min, max);
+            while (i == _lastIndex)
+            {
+                i = r.Next(min, max);
+            }
+            _lastIndex = i;
+            return i;
+        }
 
-        public ImageSource ImageSource
+        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
         {
-            get
+            var messageBoxResult = MessageBox.Show("确定退出吗?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
+            if (messageBoxResult == MessageBoxResult.Cancel)
+            {
+                e.Cancel = true;
+            }
+            else
             {
-                return imageSource;
+                Application.Current.Shutdown();
             }
         }
 
@@ -68,8 +93,7 @@ namespace FaceChange
         private void FullScreen_Click(object sender, RoutedEventArgs e)
         {
             ToolBar.Visibility = Visibility.Hidden;
-            Thickness thickness = new Thickness {Top = 0};
-            //image.Margin = thickness;
+            Face.Margin = new Thickness {Left = 0, Top = 0, Right = 0, Bottom = 0};
             this.Topmost = false;
             this.WindowStyle = WindowStyle.None;
             this.WindowState = WindowState.Maximized;

+ 18 - 42
FaceChange/SettingsWindow.xaml.cs

@@ -4,10 +4,8 @@ using System.Drawing;
 using System.Drawing.Imaging;
 using System.IO;
 using System.IO.Ports;
-using System.Linq;
 using System.Windows;
 using System.Windows.Forms;
-using System.Windows.Media;
 using System.Windows.Media.Imaging;
 
 namespace FaceChange
@@ -17,24 +15,24 @@ namespace FaceChange
     /// </summary>
     public partial class SettingsWindow : Window
     {
-        AppContext appContext = AppContext.GetAppContext();
-        SerialTool serialTool = SerialTool.GetSerialTool();
+        readonly AppContext _appContext = AppContext.GetAppContext();
+        readonly SerialTool _serialTool = SerialTool.GetSerialTool();
 
-        private List<FaceInfo> faces;
+        private List<FaceInfo> _faces;
 
         public SettingsWindow()
         {
             InitializeComponent();
 
-            faces = appContext.FaceInfos;
+            _faces = _appContext.FaceInfos;
         }
 
         private void Window_Loaded(object sender, RoutedEventArgs e)
         {
-            if (appContext.PortOpen)
+            if (_appContext.PortOpen)
             {
                 Devices.Items.Clear();
-                Devices.Items.Add(appContext.SerialPort.PortName);
+                Devices.Items.Add(_appContext.SerialPort.PortName);
                 Devices.SelectedIndex = 0;
                 Devices.IsEnabled = false;
                 Connect.Content = "断开连接";
@@ -45,31 +43,9 @@ namespace FaceChange
                 Connect.Content = "连接设备";
                 Scan_Click(null, null);
             }
-            // 读取配置
-            var prop = Properties.Settings.Default.Faces;
-            if (prop != null && !prop.Trim().Equals(""))
+            foreach (var face in _faces)
             {
-                var faceStringList = prop.Split('|').ToList();
-                foreach (var faceString in faceStringList)
-                {
-                    var fileName = faceString.Substring(
-                        faceString.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
-                    try
-                    {
-                        faces.Add(new FaceInfo(
-                            new BitmapImage(new Uri(faceString)),
-                            fileName,
-                            faceString.Replace(fileName, @"")));
-                    }
-                    catch (Exception exception)
-                    {
-                        Console.WriteLine(exception);
-                    }
-                }
-                foreach (var face in faces)
-                {
-                    ListBox.Items.Add(face);
-                }
+                ListBox.Items.Add(face);
             }
             FaceCount.Content = ListBox.Items.Count.ToString();
         }
@@ -77,7 +53,7 @@ namespace FaceChange
         private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
         {
             var list = new List<string>();
-            foreach (var face in faces)
+            foreach (var face in _faces)
             {
                 list.Add(face.Path + face.Name);
             }
@@ -106,7 +82,7 @@ namespace FaceChange
         {
             // 清除当前串口号中的所有串口名称
             Devices.Items.Clear();
-            var portList = serialTool.FindPort();
+            var portList = _serialTool.FindPort();
             if (portList.Count == 0)
             {
                 System.Windows.Forms.MessageBox.Show("没有找到可用设备!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
@@ -124,16 +100,16 @@ namespace FaceChange
 
         private void Connect_Click(object sender, RoutedEventArgs e)
         {
-            if (appContext.PortOpen)
+            if (_appContext.PortOpen)
             {
                 // 关闭串口
-                if (serialTool.ClosePort(appContext.SerialPort))
+                if (_serialTool.ClosePort(_appContext.SerialPort))
                 {
                     Devices.IsEnabled = true;
                     Scan.IsEnabled = true;
                     Scan_Click(null, null);
                     Connect.Content = "连接设备";
-                    appContext.SerialPort = null;
+                    _appContext.SerialPort = null;
                 }
                 else
                 {
@@ -150,7 +126,7 @@ namespace FaceChange
                     return;
                 }
                 // 打开串口
-                SerialPort serialPort = serialTool.OpenPort(portName, "9600", "8", "1", "无", appContext.DataReceived);
+                SerialPort serialPort = _serialTool.OpenPort(portName, "9600", "8", "1", "无", _appContext.DataReceived);
                 if (serialPort == null)
                 {
                     // 打开串口失败
@@ -161,7 +137,7 @@ namespace FaceChange
                     Devices.IsEnabled = false;
                     Scan.IsEnabled = false;
                     Connect.Content = "断开连接";
-                    appContext.SerialPort = serialPort;
+                    _appContext.SerialPort = serialPort;
                 }
             }
         }
@@ -174,7 +150,7 @@ namespace FaceChange
                 System.Windows.Forms.MessageBox.Show("请先选一项!", "提示", MessageBoxButtons.OK);
                 return;
             }
-            faces.RemoveAt(index);
+            _faces.RemoveAt(index);
             ListBox.Items.RemoveAt(index);
             FaceCount.Content = ListBox.Items.Count.ToString();
         }
@@ -184,7 +160,7 @@ namespace FaceChange
             var messageBoxButtons = MessageBoxButtons.OKCancel;
             var dr = System.Windows.Forms.MessageBox.Show("将清空列表,确定吗?", "提示", messageBoxButtons, MessageBoxIcon.Warning);
             if (dr != System.Windows.Forms.DialogResult.OK) return;
-            faces.Clear();
+            _faces.Clear();
             ListBox.Items.Clear();
             FaceCount.Content = ListBox.Items.Count.ToString();
         }
@@ -210,7 +186,7 @@ namespace FaceChange
                     fileName,
                     strFileName.Replace(fileName, @""));
                 ListBox.Items.Add(faceInfo);
-                faces.Add(faceInfo);
+                _faces.Add(faceInfo);
             }
             FaceCount.Content = ListBox.Items.Count.ToString();
         }