终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

58 lines
1.8 KiB

  1. using Model;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace BPASmartClient.MilkWithTea
  11. {
  12. public class GLobal
  13. {
  14. //路径
  15. public static string recipePath = string.Empty;
  16. public static string posionPath = string.Empty;
  17. public static bool makeEnable = false;
  18. public static ObservableCollection<LocalTeaWithMilkConfig> MaterialRecipes { get; set; } = new ObservableCollection<LocalTeaWithMilkConfig>();
  19. /// <summary>
  20. /// 获取Json文件内容,转换成ObservableCollection
  21. /// </summary>
  22. /// <typeparam name="T"></typeparam>
  23. /// <param name="path"></param>
  24. /// <returns></returns>
  25. public static ObservableCollection<T> GetJsonToT<T>(string path)
  26. {
  27. if (!File.Exists(path))
  28. {
  29. //创建该文件
  30. File.Create(path);
  31. return default;
  32. }
  33. else
  34. {
  35. using (StreamReader recipeReader = new StreamReader(path))//读取json文件
  36. {
  37. string datacache = "";
  38. string line;
  39. while ((line = recipeReader.ReadLine()) != null) //循环将每一行数据拼接为一个完整的字符串
  40. {
  41. datacache = datacache + line;
  42. }
  43. var res = JsonConvert.DeserializeObject<ObservableCollection<T>>(datacache); //将string转换为class类,从而达到json文件转换的目的
  44. if (res != null)
  45. return res;
  46. else return new ObservableCollection<T> { };
  47. }
  48. }
  49. }
  50. }
  51. }