Browse Source

1.完善主窗口逻辑;
2.完善接收数据处理逻辑。

Yumin 6 years ago
parent
commit
da73bf8bc0
3 changed files with 64 additions and 42 deletions
  1. 50 1
      FaceChange/AppContext.cs
  2. 1 1
      FaceChange/MainWindow.xaml
  3. 13 40
      FaceChange/MainWindow.xaml.cs

+ 50 - 1
FaceChange/AppContext.cs

@@ -3,14 +3,17 @@ using System.Collections.Generic;
 using System.IO.Ports;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Windows.Forms;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
+using MessageBox = System.Windows.Forms.MessageBox;
 
 namespace FaceChange
 {
     internal class AppContext
     {
+        private SynchronizationContext _synchronizationContext;
         // 串口类
         private SerialPort _serialPort;
         // Data
@@ -22,7 +25,14 @@ namespace FaceChange
 
         public static AppContext GetAppContext()
         {
-            return _appContext ?? (_appContext = new AppContext());
+            if (_appContext == null)
+            {
+                _appContext = new AppContext();
+                _appContext._faces = _appContext.FaceInfos;
+                // 获取UI线程同步上下文
+                _appContext._synchronizationContext = SynchronizationContext.Current;
+            }
+            return _appContext;
         }
 
         public SerialPort SerialPort
@@ -87,6 +97,23 @@ namespace FaceChange
             }
         }
 
+        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 void DataReceived(object sender, SerialDataReceivedEventArgs e)
         {
             try
@@ -99,11 +126,33 @@ namespace FaceChange
                 // 处理 readBuffer 中的数据,自定义处理过程
                 var msg = Encoding.Default.GetString(readBuffer, 0, len);
                 Console.WriteLine(msg);
+                // 委托执行
+                var index = GetNoRepeatRandom(0, _faces.Count);
+                ThreadProcSafePost(index);
             }
             catch (Exception ex)
             {
                 MessageBox.Show("接收数据异常!具体原因:" + ex.Message, "错误提示");
             }
         }
+
+        private void ThreadProcSafePost(int text)
+        {
+            _synchronizationContext.Post(SetTextSafePost, text);
+        }
+
+        private void SetTextSafePost(object index)
+        {
+            if (_faces.Count == 0)
+            {
+                MessageBox.Show("请先在设置页添加资源", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
+            }
+            else
+            {
+                MainWindow.FaceName.Content = _faces[(int)index].Name;
+                MainWindow.FaceSrc.Source = _faces[(int)index].Face;
+                Console.WriteLine(index);
+            }
+        }
     }
 }

+ 1 - 1
FaceChange/MainWindow.xaml

@@ -14,7 +14,7 @@
             <Separator/>
             <Label x:Name="PortStatus" Content="未连接" ToolTip="设备连接" Foreground="Red"/>
             <Separator/>
-            <Label x:Name="Name" Content="" Foreground="Gray"/>
+            <Label x:Name="FileName" Content="" Foreground="Gray"/>
         </ToolBar>
         <Image x:Name="Face" Margin="0,32,0,0"/>
     </Grid>

+ 13 - 40
FaceChange/MainWindow.xaml.cs

@@ -14,18 +14,20 @@ namespace FaceChange
     {
         private readonly AppContext _appContext = AppContext.GetAppContext();
         private readonly SerialTool _serialTool = SerialTool.GetSerialTool();
-
-        private readonly List<FaceInfo> _faces;
+        
         private bool _noDevice;
 
         public static Label Status;
+        public static Label FaceName;
+        public static Image FaceSrc;
 
         public MainWindow()
         {
             InitializeComponent();
-
-            _faces = _appContext.FaceInfos;
+            
             Status = PortStatus;
+            FaceName = FileName;
+            FaceSrc = Face;
             LoadDevice();
         }
 
@@ -38,44 +40,15 @@ namespace FaceChange
 
         private void Window_KeyDown(object sender, KeyEventArgs e)
         {
-            if (e.Key == Key.Escape)
-            {
-                ToolBar.Visibility = Visibility.Visible;
-                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)
+            switch (e.Key)
             {
-                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);
+                case Key.Escape:
+                    ToolBar.Visibility = Visibility.Visible;
+                    Face.Margin = new Thickness { Left = 0, Top = 32, Right = 0, Bottom = 0 };
+                    WindowState = WindowState.Normal;
+                    WindowStyle = WindowStyle.SingleBorderWindow;
+                    break;
             }
-            _lastIndex = i;
-            return i;
         }
 
         private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)