AdaptiveText.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 Microsoft.Toolkit.Uwp.Notifications.Adaptive.Elements;
  16. namespace Microsoft.Toolkit.Uwp.Notifications
  17. {
  18. /// <summary>
  19. /// An adaptive text element.
  20. /// </summary>
  21. public sealed class AdaptiveText
  22. : IAdaptiveChild,
  23. IAdaptiveSubgroupChild,
  24. ITileBindingContentAdaptiveChild,
  25. IToastBindingGenericChild
  26. {
  27. #if WINRT
  28. /// <summary>
  29. /// Gets a dictionary of the current data bindings, where you can assign new bindings.
  30. /// </summary>
  31. public IDictionary<AdaptiveTextBindableProperty, string> Bindings { get; private set; } = new Dictionary<AdaptiveTextBindableProperty, string>();
  32. #endif
  33. /// <summary>
  34. /// The text to display. Data binding support added in Creators Update, only works for toast top-level text elements.
  35. /// </summary>
  36. public
  37. #if WINRT
  38. string
  39. #else
  40. BindableString
  41. #endif
  42. Text { get; set; }
  43. /// <summary>
  44. /// The target locale of the XML payload, specified as a BCP-47 language tags such as "en-US" or "fr-FR". The locale specified here overrides any other specified locale, such as that in binding or visual. If this value is a literal string, this attribute defaults to the user's UI language. If this value is a string reference, this attribute defaults to the locale chosen by Windows Runtime in resolving the string.
  45. /// </summary>
  46. public string Language { get; set; }
  47. /// <summary>
  48. /// The style controls the text's font size, weight, and opacity. Note that for Toast, the style will only take effect if the text is inside an <see cref="AdaptiveSubgroup"/>.
  49. /// </summary>
  50. public AdaptiveTextStyle HintStyle { get; set; }
  51. /// <summary>
  52. /// Set this to true to enable text wrapping. For Tiles, this is false by default. For Toasts, this is true on top-level text elements, and false inside an <see cref="AdaptiveSubgroup"/>. Note that for Toast, setting wrap will only take effect if the text is inside an <see cref="AdaptiveSubgroup"/> (you can use HintMaxLines = 1 to prevent top-level text elements from wrapping).
  53. /// </summary>
  54. public bool? HintWrap { get; set; }
  55. private int? _hintMaxLines;
  56. /// <summary>
  57. /// The maximum number of lines the text element is allowed to display. For Tiles, this is infinity by default. For Toasts, top-level text elements will have varying max line amounts (and in the Anniversary Update you can change the max lines). Text on a Toast inside an <see cref="AdaptiveSubgroup"/> will behave identically to Tiles (default to infinity).
  58. /// </summary>
  59. public int? HintMaxLines
  60. {
  61. get
  62. {
  63. return _hintMaxLines;
  64. }
  65. set
  66. {
  67. if (value != null)
  68. {
  69. Element_AdaptiveText.CheckMaxLinesValue(value.Value);
  70. }
  71. _hintMaxLines = value;
  72. }
  73. }
  74. private int? _hintMinLines;
  75. /// <summary>
  76. /// The minimum number of lines the text element must display. Note that for Toast, this property will only take effect if the text is inside an <see cref="AdaptiveSubgroup"/>.
  77. /// </summary>
  78. public int? HintMinLines
  79. {
  80. get
  81. {
  82. return _hintMinLines;
  83. }
  84. set
  85. {
  86. if (value != null)
  87. {
  88. Element_AdaptiveText.CheckMinLinesValue(value.Value);
  89. }
  90. _hintMinLines = value;
  91. }
  92. }
  93. /// <summary>
  94. /// The horizontal alignment of the text. Note that for Toast, this property will only take effect if the text is inside an <see cref="AdaptiveSubgroup"/>.
  95. /// </summary>
  96. public AdaptiveTextAlign HintAlign { get; set; }
  97. internal Element_AdaptiveText ConvertToElement()
  98. {
  99. var answer = new Element_AdaptiveText()
  100. {
  101. Lang = Language,
  102. Style = HintStyle,
  103. Wrap = HintWrap,
  104. MaxLines = HintMaxLines,
  105. MinLines = HintMinLines,
  106. Align = HintAlign
  107. };
  108. #if WINRT
  109. answer.Text = XmlWriterHelper.GetBindingOrAbsoluteXmlValue(Bindings, AdaptiveTextBindableProperty.Text, Text);
  110. #else
  111. answer.Text = Text?.ToXmlString();
  112. #endif
  113. return answer;
  114. }
  115. /// <summary>
  116. /// Returns the value of the Text property.
  117. /// </summary>
  118. /// <returns>The value of the Text property.</returns>
  119. public override string ToString()
  120. {
  121. return Text;
  122. }
  123. }
  124. }