AdaptiveProgressBar.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // ******************************************************************
  2. // Copyright (c) Microsoft. All rights reserved.
  3. // This code is licensed under the MIT License (MIT).
  4. // THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  5. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  6. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  7. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  8. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  9. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  10. // THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
  11. // ******************************************************************
  12. #if WINRT
  13. using System.Collections.Generic;
  14. #endif
  15. using System;
  16. using Microsoft.Toolkit.Uwp.Notifications.Adaptive.Elements;
  17. namespace Microsoft.Toolkit.Uwp.Notifications
  18. {
  19. /// <summary>
  20. /// New in Creators Update: A progress bar. Only supported on toasts on Desktop, build 15007 or newer.
  21. /// </summary>
  22. public sealed class AdaptiveProgressBar : IToastBindingGenericChild
  23. {
  24. #if WINRT
  25. /// <summary>
  26. /// Gets a dictionary of the current data bindings, where you can assign new bindings.
  27. /// </summary>
  28. public IDictionary<AdaptiveProgressBarBindableProperty, string> Bindings { get; private set; } = new Dictionary<AdaptiveProgressBarBindableProperty, string>();
  29. #endif
  30. /// <summary>
  31. /// Gets or sets an optional title string. Supports data binding.
  32. /// </summary>
  33. public
  34. #if WINRT
  35. string
  36. #else
  37. BindableString
  38. #endif
  39. Title { get; set; }
  40. /// <summary>
  41. /// Gets or sets the value of the progress bar. Supports data binding. Defaults to 0.
  42. /// </summary>
  43. public
  44. #if WINRT
  45. AdaptiveProgressBarValue
  46. #else
  47. BindableProgressBarValue
  48. #endif
  49. Value { get; set; } = AdaptiveProgressBarValue.FromValue(0);
  50. /// <summary>
  51. /// Gets or sets an optional string to be displayed instead of the default percentage string. If this isn't provided, something like "70%" will be displayed.
  52. /// </summary>
  53. public
  54. #if WINRT
  55. string
  56. #else
  57. BindableString
  58. #endif
  59. ValueStringOverride { get; set; }
  60. /// <summary>
  61. /// Required. Gets or sets a status string, which is displayed underneath the progress bar. This string should reflect the status of the operation, like "Downloading..." or "Installing..."
  62. /// </summary>
  63. public
  64. #if WINRT
  65. string
  66. #else
  67. BindableString
  68. #endif
  69. Status { get; set; }
  70. internal Element_AdaptiveProgressBar ConvertToElement()
  71. {
  72. // If Value not provided, we use 0
  73. var val = Value;
  74. if (val == null)
  75. {
  76. val = AdaptiveProgressBarValue.FromValue(0);
  77. }
  78. var answer = new Element_AdaptiveProgressBar();
  79. #if WINRT
  80. answer.Title = XmlWriterHelper.GetBindingOrAbsoluteXmlValue(Bindings, AdaptiveProgressBarBindableProperty.Title, Title);
  81. answer.Value = XmlWriterHelper.GetBindingOrAbsoluteXmlValue(Bindings, AdaptiveProgressBarBindableProperty.Value, val.ToXmlString());
  82. answer.ValueStringOverride = XmlWriterHelper.GetBindingOrAbsoluteXmlValue(Bindings, AdaptiveProgressBarBindableProperty.ValueStringOverride, ValueStringOverride);
  83. answer.Status = XmlWriterHelper.GetBindingOrAbsoluteXmlValue(Bindings, AdaptiveProgressBarBindableProperty.Status, Status);
  84. #else
  85. answer.Title = Title?.ToXmlString();
  86. answer.Value = val.ToXmlString();
  87. answer.ValueStringOverride = ValueStringOverride?.ToXmlString();
  88. answer.Status = Status?.ToXmlString();
  89. #endif
  90. if (answer.Status == null)
  91. {
  92. throw new NullReferenceException("Status property is required.");
  93. }
  94. return answer;
  95. }
  96. }
  97. }