DesignTimeHelper.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace Uni_Trend.MSO7000X.Common.Helper
  11. {
  12. public static class DesignTimeHelper
  13. {
  14. private static Boolean? _isAssemblyVisualStudio;
  15. private static Boolean? _isLicenseDesignTime;
  16. private static Boolean? _isProcessDevEnv;
  17. private static Boolean? _mIsDesignerHosted;
  18. /// <summary>
  19. /// Property <see cref="Form.DesignMode"/> does not correctly report if a nested <see cref="UserControl"/>
  20. /// is in design mode. InDesignMode is a corrected that property which .
  21. /// (see https://connect.microsoft.com/VisualStudio/feedback/details/553305
  22. /// and https://stackoverflow.com/a/2693338/238419 )
  23. /// </summary>
  24. public static Boolean InDesignMode(this Control userControl, String source = null)
  25. => IsLicenseDesignTime
  26. || IsProcessDevEnv
  27. || IsExecutingAssemblyVisualStudio
  28. || IsDesignerHosted(userControl);
  29. private static Boolean IsExecutingAssemblyVisualStudio
  30. => _isAssemblyVisualStudio
  31. ?? (_isAssemblyVisualStudio = Assembly
  32. .GetExecutingAssembly()
  33. .Location.Contains(value: "VisualStudio"))
  34. .Value;
  35. private static Boolean IsLicenseDesignTime
  36. => _isLicenseDesignTime
  37. ?? (_isLicenseDesignTime = LicenseManager.UsageMode == LicenseUsageMode.Designtime)
  38. .Value;
  39. private static Boolean IsDesignerHosted(
  40. Control control)
  41. {
  42. if (_mIsDesignerHosted.HasValue)
  43. return _mIsDesignerHosted.Value;
  44. while (control != null)
  45. {
  46. if (control.Site?.DesignMode == true)
  47. {
  48. _mIsDesignerHosted = true;
  49. return true;
  50. }
  51. control = control.Parent;
  52. }
  53. _mIsDesignerHosted = false;
  54. return false;
  55. }
  56. private static Boolean IsProcessDevEnv
  57. => _isProcessDevEnv
  58. ?? (_isProcessDevEnv = Process.GetCurrentProcess()
  59. .ProcessName == "devenv")
  60. .Value;
  61. }
  62. }