ResultMessage.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using FWindSoft.Data;
  2. using FWindSoft.Wpf.Controls;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. namespace FWindSoft.Wpf.Common
  10. {
  11. public class ResultMessage
  12. {
  13. #region 窗体附加参数维护
  14. public static readonly DependencyProperty ResultProperty = DependencyProperty.RegisterAttached("Result",
  15. typeof(DataWrapper), typeof(ResultMessage), new PropertyMetadata(null, null));
  16. [AttachedPropertyBrowsableForType(typeof(UIElement))]
  17. public static DataWrapper GetResult(DependencyObject obj)
  18. {
  19. return obj.GetValue(ResultProperty) as DataWrapper;
  20. }
  21. [AttachedPropertyBrowsableForType(typeof(UIElement))]
  22. public static void SetResult(DependencyObject obj, DataWrapper value)
  23. {
  24. obj.SetValue(ResultProperty, value);
  25. }
  26. static ResultMessage()
  27. {
  28. }
  29. #endregion
  30. }
  31. public static class ResultMessageExtension
  32. {
  33. public static DataWrapper GetResult(this UIElement baseWindow)
  34. {
  35. return ResultMessage.GetResult(baseWindow);
  36. }
  37. public static void SetResult(this UIElement baseWindow,DataWrapper wrapper)
  38. {
  39. ResultMessage.SetResult(baseWindow, wrapper);
  40. }
  41. /// <summary>
  42. /// 获取关联真实值
  43. /// </summary>
  44. /// <param name="baseWindow"></param>
  45. /// <returns></returns>
  46. public static object GetRealResult(this UIElement baseWindow)
  47. {
  48. var dataWrapper= ResultMessage.GetResult(baseWindow) as DataWrapper;
  49. if (dataWrapper == null)
  50. return null;
  51. return dataWrapper.Parameter;
  52. }
  53. public static T GetRealResult<T>(this UIElement baseWindow)
  54. {
  55. var dataWrapper = ResultMessage.GetResult(baseWindow) as DataWrapper;
  56. if (dataWrapper == null)
  57. return default(T);
  58. return dataWrapper.GetValue<T>();
  59. }
  60. /// <summary>
  61. /// 设置关联真实值
  62. /// </summary>
  63. /// <param name="baseWindow"></param>
  64. /// <param name="realValue"></param>
  65. public static void SetRealResult(this UIElement baseWindow, object realValue)
  66. {
  67. var dataWrapper = new DataWrapper(realValue);
  68. ResultMessage.SetResult(baseWindow, dataWrapper);
  69. }
  70. public static void SetRealResult<T>(this UIElement baseWindow, T realValue)
  71. {
  72. SetRealResult(baseWindow, (object)realValue);
  73. }
  74. }
  75. }