diff --git a/View/DispenseMachinesSet.xaml b/View/DispenseMachinesSet.xaml index b26041c..49bb0c4 100644 --- a/View/DispenseMachinesSet.xaml +++ b/View/DispenseMachinesSet.xaml @@ -62,15 +62,14 @@ - - - + diff --git a/View/DispenseMachinesSet.xaml.cs b/View/DispenseMachinesSet.xaml.cs index 104d5e5..afbddb8 100644 --- a/View/DispenseMachinesSet.xaml.cs +++ b/View/DispenseMachinesSet.xaml.cs @@ -5,6 +5,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Data; +using System.IO; using System.Linq; using System.Net; using System.Net.NetworkInformation; @@ -45,7 +46,10 @@ namespace SunlightCentralizedControlManagement_SCCM_.View //private string SYS_machines = null; private void UserControl_Loaded(object sender, RoutedEventArgs e) { - Griddata.ItemsSource = MainWindowViewModel.Machines.DefaultView; + SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 + SQLiteHelpers.Open(); //打开数据库 + Griddata.ItemsSource = SQLiteHelpers.ExecuteDataSet("select Name,Groups,IP,PORT,DATA from MachinesDispense Order by id", null).Tables[0].DefaultView; + SQLiteHelpers.Close(); } private void Griddata_MouseDoubleClick(object sender, MouseButtonEventArgs e) @@ -63,35 +67,37 @@ namespace SunlightCentralizedControlManagement_SCCM_.View private void Save_Click(object sender, RoutedEventArgs e) { - if (!string.IsNullOrEmpty(comboBoxMachine.Text)) + if (!string.IsNullOrEmpty(comboBoxMachine.Text) && !string.IsNullOrEmpty(TextMachineGroup.Text) && !string.IsNullOrEmpty(IP .Text)) { + WriteableBitmap wb = new WriteableBitmap((BitmapSource)IMAGE.Source); + byte[] imageData = SourceToArray(wb); + Dictionary dr_new = new Dictionary();//缓存函数 dr_new.Add("Name", comboBoxMachine.Text); dr_new.Add("Groups", TextMachineGroup.Text); dr_new.Add("IP", IP.Text); dr_new.Add("PORT", PORT.Text); + dr_new.Add("DATA",imageData); SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 SQLiteHelpers.Open(); //打开数据库 - if (SQLiteHelpers.ExecuteDataSet("select * from Machines where name ='" + comboBoxMachine.Text + "'", null).Tables[0].Rows.Count == 0) + if (SQLiteHelpers.ExecuteDataSet("select * from MachinesDispense where name ='" + comboBoxMachine.Text + "'", null).Tables[0].Rows.Count == 0) { - SQLiteHelpers.InsertData("Machines", dr_new); + SQLiteHelpers.InsertData("MachinesDispense", dr_new); } else { - SQLiteHelpers.Update("Machines", dr_new, "name='" + comboBoxMachine.Text + "'", null); + SQLiteHelpers.Update("MachinesDispense", dr_new, "name='" + comboBoxMachine.Text + "'", null); } - MainWindowViewModel.Machines = SQLiteHelpers.ExecuteDataSet("select * from Machines Order by id", null).Tables[0]; //读取表写入缓存 + Griddata.ItemsSource = SQLiteHelpers.ExecuteDataSet("select Name,Groups,IP,PORT,DATA from MachinesDispense Order by id", null).Tables[0].DefaultView; SQLiteHelpers.Close(); - Griddata.ItemsSource = MainWindowViewModel.Machines.DefaultView; - comboBoxMachine.Text = null; - TextMachineGroup.Text = null; - IP.Text = null; - PORT.Text = "7789"; + MessageBox.Show(Properties.Resources.Save + comboBoxMachine.Text, "SCCM", MessageBoxButton.OK, MessageBoxImage.Information); } else { + MessageBox.Show(Properties.Resources.ERR , "SCCM", MessageBoxButton.OK, MessageBoxImage.Error); + } } private void Delete_Click(object sender, RoutedEventArgs e) @@ -103,10 +109,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.View { SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 SQLiteHelpers.Open(); //打开数据库 - SQLiteHelpers.Delete("Machines", "Name='" + comboBoxMachine.Text + "'", null); - MainWindowViewModel.Machines = SQLiteHelpers.ExecuteDataSet("select * from Machines Order by id", null).Tables[0]; //读取表写入缓存 + SQLiteHelpers.Delete("MachinesDispense", "Name='" + comboBoxMachine.Text + "'", null); + Griddata.ItemsSource = SQLiteHelpers.ExecuteDataSet("select Name,Groups,IP,PORT,DATA from MachinesDispense Order by id", null).Tables[0].DefaultView; SQLiteHelpers.Close(); - Griddata.ItemsSource = MainWindowViewModel.Machines.DefaultView; comboBoxMachine.Text = null; } } @@ -171,42 +176,42 @@ namespace SunlightCentralizedControlManagement_SCCM_.View switch (TextMachineGroup.Text)//步骤用时计算 { case "252": - uri = new Uri("pack://application:,,,/ReferencedAssembly;component/LOGO/252-2t.png"); + uri = new Uri("pack://application:,,,/LOGO/252-2t.png"); bitmapImage = new BitmapImage(uri); IMAGE.Source = bitmapImage; break; case "252-2T": - uri = new Uri("pack://application:,,,/ReferencedAssembly;component/SunlightCentralizedControlManagement(SCCM)/LOGO/252-2t.png"); + uri = new Uri("pack://application:,,,/LOGO/252-2t.png"); bitmapImage = new BitmapImage(uri); IMAGE.Source = bitmapImage; break; case "252RB": - uri = new Uri("pack://application:,,,/ReferencedAssembly;component/SunlightCentralizedControlManagement(SCCM)/LOGO/252RB.png"); + uri = new Uri("pack://application:,,,/LOGO/252RB.png"); bitmapImage = new BitmapImage(uri); IMAGE.Source = bitmapImage; break; case "252RDRM": - uri = new Uri("pack://application:,,,/ReferencedAssembly;component/SunlightCentralizedControlManagement(SCCM)/LOGO/252RDRM.png"); + uri = new Uri("pack://application:,,,/LOGO/252RMRD.png"); bitmapImage = new BitmapImage(uri); IMAGE.Source = bitmapImage; break; case "242W": - uri = new Uri("pack://application:,,,/SunlightCentralizedControlManagement_SCCM_;component/242w.png"); + uri = new Uri("pack://application:,,,/LOGO/242w.png"); bitmapImage = new BitmapImage(uri); IMAGE.Source = bitmapImage; break; case "302": - uri = new Uri("pack://application:,,,/ReferencedAssembly;component/SunlightCentralizedControlManagement(SCCM)/LOGO/302.png"); + uri = new Uri("pack://application:,,,/LOGO/302.png"); bitmapImage = new BitmapImage(uri); IMAGE.Source = bitmapImage; break; case "303": - uri = new Uri("pack://application:,,,/ReferencedAssembly;component/SunlightCentralizedControlManagement(SCCM)/LOGO/303.png"); + uri = new Uri("pack://application:,,,/LOGO/303.png"); bitmapImage = new BitmapImage(uri); IMAGE.Source = bitmapImage; break; case "303PDW": - uri = new Uri("pack://application:,,,/ReferencedAssembly;component/SunlightCentralizedControlManagement(SCCM)/LOGO/303pdw.png"); + uri = new Uri("pack://application:,,,/LOGO/303pdw.png"); bitmapImage = new BitmapImage(uri); IMAGE.Source = bitmapImage; break; @@ -214,5 +219,50 @@ namespace SunlightCentralizedControlManagement_SCCM_.View break; } } + + public static byte[] BitmapImageToByteArray(BitmapImage bmp) + { + byte[] byteArray = null; + try + { + Stream sMarket = bmp.StreamSource; + if (sMarket != null && sMarket.Length > 0) + { + //很重要,因为Position经常位于Stream的末尾,导致下面读取到的长度为0。 + sMarket.Position = 0; + + using (BinaryReader br = new BinaryReader(sMarket)) + { + byteArray = br.ReadBytes((int)sMarket.Length); + } + } + } + catch + { + //other exception handling + } + return byteArray; + } + + public static byte[] SourceToArray(WriteableBitmap source) + { + byte[] byteArray = null; + try + { + using (MemoryStream ms = new MemoryStream()) + { + JpegBitmapEncoder coder = new JpegBitmapEncoder(); + coder.QualityLevel = 100; + coder.Frames.Add(BitmapFrame.Create(source)); + coder.Save(ms); + byteArray = ms.ToArray(); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message.ToString()); + } + return byteArray; + } } }