ImageFileNameFormatConverter.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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("Number", () => 1);
  25. template.RegisterVariable("DateTime", () => DateTime.Now);
  26. // 生成文件名
  27. string fileName = template.Generate();
  28. return fileName;
  29. }
  30. catch (Exception)
  31. {
  32. return value.ToString();
  33. }
  34. }
  35. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  36. {
  37. throw new NotImplementedException();
  38. }
  39. }
  40. }