123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- /////////////////////////////////////////////
- //Copyright (c) 2016, SAGA
- //All rights reserved.
- //文件名称: WindowHistoryData.cs
- //文件描述: 历史数据保存
- //创 建 者: 李勇
- //创建日期: 2016-03-02
- //版 本 号:1.0.0.0
- /////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Security.Cryptography;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Security;
- namespace SAGA.DotNetUtils
- {
- public static class WindowHistoryData
- {
- /// <summary>
- /// 保存控件数据
- /// </summary>
- public static void SaveControlData(this Window win)
- {
- MD5 md5 = new MD5CryptoServiceProvider();
- md5.ComputeHash(Encoding.Default.GetBytes(win.GetType().FullName + win.Title));
- string winFileName =string.Join("", md5.Hash.Select(b=>b.ToString()));
- var path = Path.Combine(AppBaseInfo.AppTempFilePath,
- @"HistoryData\" + winFileName + ".txt");
- var datas = new List<SaveData>();
- GetControlData(win, datas);
- string saveData = SerializerByDataContract.ObjectToJson(datas);
- if (!Directory.Exists(Path.GetDirectoryName(path)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(path));
- }
- File.WriteAllText(path, saveData, Encoding.Default);
- }
- /// <summary>
- /// 设置控件数据
- /// </summary>
- public static void SetControlData(this Window win)
- {
- MD5 md5 = new MD5CryptoServiceProvider();
- md5.ComputeHash(Encoding.Default.GetBytes(win.GetType().FullName+win.Title));
- string winFileName = string.Join("", md5.Hash.Select(b => (b).ToString()));
- var path = Path.Combine(AppBaseInfo.AppTempFilePath,
- @"HistoryData\" + winFileName + ".txt");
- if (File.Exists(path))
- {
- string str = File.ReadAllText(path, Encoding.Default);
- var datas = SerializerByDataContract.JsonToObj<List<SaveData>>(str);
- SetControlData(win, datas);
- }
- }
- /// <summary>
- /// 获取数据
- /// </summary>
- /// <param name="visual"></param>
- /// <param name="datas"></param>
- private static void GetControlData(Visual visual, List<SaveData> datas)
- {
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
- {
- Visual childVisual = (Visual)VisualTreeHelper.GetChild(visual, i);
- if (childVisual != null)
- {
- if (childVisual is TextBox)
- {
- var txt = childVisual as TextBox;
- if ("".Equals(txt.Name) || txt.Name.Contains("_"))
- continue;
- datas.Add(new SaveData("", "TextBox", txt.Name, txt.Text));
- }
- else if (childVisual is ComboBox)
- {
- var cmb = childVisual as ComboBox;
- if ("".Equals(cmb.Name) || cmb.Name.Contains("_"))
- continue;
- datas.Add(new SaveData("", "ComboBox", cmb.Name, cmb.Text));
- }
- else if (childVisual is RadioButton)
- {
- var cmb = childVisual as RadioButton;
- if ("".Equals(cmb.Name) || cmb.Name.Contains("_"))
- continue;
- datas.Add(new SaveData("", "RadioButton", cmb.Name, cmb.IsChecked.ToString()));
- }
- else if (childVisual is CheckBox)
- {
- var cmb = childVisual as CheckBox;
- if ("".Equals(cmb.Name) || cmb.Name.Contains("_"))
- continue;
- datas.Add(new SaveData("", "CheckBox", cmb.Name, cmb.IsChecked.ToString()));
- }
- else if (childVisual is ListBox)
- {
- var cmb = childVisual as ListBox;
- if ("".Equals(cmb.Name) || cmb.Name.Contains("_"))
- continue;
- datas.Add(new SaveData("", "ListBox", cmb.Name, cmb.SelectedIndex.ToString()));
- }
- //else if (visual is Grid)
- //{
- // var g = visual as Grid;
- // foreach (var c in g.Children)
- // {
- // Trace.WriteLine(" BBB"+c.ToString());
- // }
- //}
- GetControlData(childVisual, datas);
- }
- }
- }
- public static void SetControlData(this Window win, out List<SaveData> datas)
- {
- datas = new List<SaveData>();
- MD5 md5 = new MD5CryptoServiceProvider();
- md5.ComputeHash(Encoding.Default.GetBytes(win.GetType().FullName + win.Title));
- string winFileName = string.Join("", md5.Hash.Select(b => (b).ToString()));
- var path = Path.Combine(AppBaseInfo.AppTempFilePath,
- @"HistoryData\" + winFileName + ".txt");
- if (File.Exists(path))
- {
- string str = File.ReadAllText(path, Encoding.Default);
- datas = SerializerByDataContract.JsonToObj<List<SaveData>>(str);
- SetControlData((Visual)win, (List<SaveData>)datas);
- }
- }
- /// <summary>
- /// 设置数据
- /// </summary>
- /// <param name="visual"></param>
- /// <param name="datas"></param>
- private static void SetControlData(Visual visual, List<SaveData> datas)
- {
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
- {
- Visual childVisual = (Visual)VisualTreeHelper.GetChild(visual, i);
- if (childVisual != null)
- {
- if (childVisual is TextBox)
- {
- var txt = childVisual as TextBox;
- if (txt.Name != null&&!"".Equals(txt.Name))
- {
- var data = datas.FirstOrDefault(t => t.ControlId == txt.Name) ?? new SaveData();
- if (data.ControlValue.IsNotNullEmpty())
- txt.Text = data.ControlValue;
-
- }
- }
- else if (childVisual is ComboBox)
- {
- var cmb = childVisual as ComboBox;
- if (cmb.Name != null && !"".Equals(cmb.Name))
- {
- var data = datas.FirstOrDefault(t => t.ControlId == cmb.Name) ?? new SaveData();
- if (data.ControlValue.IsNotNullEmpty())
- {
- cmb.Text = data.ControlValue;
- if (cmb.SelectedIndex==-1&&cmb.Items.Count>0&&!cmb.IsEditable)
- {
- cmb.SelectedIndex=0;
- }
- // cmb.Text = data.ControlValue;
- }
-
- }
- }
- else if (childVisual is RadioButton)
- {
- var cmb = childVisual as RadioButton;
- var data = datas.FirstOrDefault(t => t.ControlId == cmb.Name);
- if (data != null && cmb.Name != null && data.ControlValue.IsNotNullEmpty())
- {
- bool isCheck = ConvertToBool(data.ControlValue);
- if(isCheck)//如果是false则不给相应radio赋值,避免不必要的界面交互造成的bug
- cmb.IsChecked = isCheck;
- }
-
- }
- else if (childVisual is CheckBox)
- {
- var cmb = childVisual as CheckBox;
- var data = datas.FirstOrDefault(t => t.ControlId == cmb.Name);
- if (data != null && cmb.Name != null && data.ControlValue!="")
- cmb.IsChecked = ConvertToBool(data.ControlValue);
- }
- else if (childVisual is ListBox)
- {
- var cmb = childVisual as ListBox;
- var data = datas.FirstOrDefault(t => t.ControlId == cmb.Name);
- if (data != null && cmb.Name != null && data.ControlValue != "")
- {
- int index = 0;
- if (int.TryParse(data.ControlValue, out index))
- {
- if (cmb.Items.Count > i)
- {
- cmb.SelectedIndex = index;
- }
-
- }
-
- }
-
-
- }
- SetControlData(childVisual, datas);
- }
- }
- }
- public static void SaveControlData(this Window win, List<SaveData> perDatas = null)
- {
- MD5 md5 = new MD5CryptoServiceProvider();
- md5.ComputeHash(Encoding.Default.GetBytes(win.GetType().FullName + win.Title));
- string winFileName = string.Join("", md5.Hash.Select(b => (b).ToString()));
- var path = Path.Combine(AppBaseInfo.AppTempFilePath,
- @"HistoryData\" + winFileName + ".txt");
- List<SaveData> datas = new List<SaveData>();
- if (perDatas != null)
- {
- datas.AddRange(perDatas);
- }
- GetControlData(win, datas);
- string contents = SerializerByDataContract.ObjectToJson(datas);
- if (!Directory.Exists(Path.GetDirectoryName(path)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(path));
- }
- File.WriteAllText(path, contents, Encoding.Default);
- }
- private static bool ConvertToBool(string str)
- {
- if (System.Text.RegularExpressions.Regex.IsMatch(str, @"True|False"))
- {
- return Convert.ToBoolean(str);
- }
- else
- {
- return false;
- }
- }
- }
- [DataContract]
- public class SaveData
- {
- public SaveData() { }
- public SaveData(string name, string type, string controlId, string value)
- {
- FormName = name;
- ControlType = type;
- ControlId = controlId;
- ControlValue = value;
- }
- [DataMember]
- public string FormName { get; set; }
- [DataMember]
- public string ControlId { get; set; }
- [DataMember]
- public string ControlType { get; set; }
- [DataMember]
- public string ControlValue { get; set; }
- }
- }
|