ImageFileNameFormatConverter.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Data;
  7. using TeamAAS_VP.Core;
  8. namespace TeamAAS_VP.ValueConverter
  9. {
  10. public class ImageFileNameFormatConverter : IValueConverter
  11. {
  12. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  13. {
  14. try
  15. {
  16. if (value == null) return string.Empty;
  17. // 创建模板,使用 {变量名:格式} 语法
  18. var template = new FileNameTemplate(value.ToString());
  19. // 注册变量
  20. template.RegisterVariable("ProductSN", () => "SN12345");
  21. template.RegisterVariable("CameraName", () => "MainCamera");
  22. template.RegisterVariable("ShotCount", () => 3);
  23. template.RegisterVariable("Result", () => "OK");
  24. template.RegisterVariable("DateTime", () => DateTime.Now);
  25. // 生成文件名
  26. string fileName = template.Generate();
  27. return fileName;
  28. }
  29. catch (Exception)
  30. {
  31. return value.ToString();
  32. }
  33. }
  34. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  35. {
  36. throw new NotImplementedException();
  37. }
  38. }
  39. }