private int GetCurrentTextLineCount() { if (!VisualChildren.Any(c => c is AdaptiveText)) { return(0); } var textList = VisualChildren.Where(c => c is AdaptiveText).Select(c => c as AdaptiveText).ToList(); // First one is already the header. // https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/adaptive-interactive-toasts#text-elements // The default (and maximum) is up to 2 lines of text for the title, and up to 4 lines (combined) for the two additional description elements (the second and third AdaptiveText). AdaptiveText text = textList.First(); int count = 0; count += text.HintMaxLines ?? 2; for (int i = 1; i < textList.Count; i++) { text = textList[i]; count += text.HintMaxLines ?? 1; } return(count); }
/// <summary> /// Add text to the toast. /// </summary> /// <param name="text">Custom text to display on the tile.</param> /// <param name="hintStyle">Style that controls the text's font size, weight, and opacity.</param> /// <param name="hintWrap">Indicating whether text wrapping is enabled. For Tiles, this is false by default.</param> /// <param name="hintMaxLines">The maximum number of lines the text element is allowed to display. For Tiles, this is infinity by default</param> /// <param name="hintMinLines">The minimum number of lines the text element must display.</param> /// <param name="hintAlign">The horizontal alignment of the text</param> /// <param name="language"> /// 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. /// </param> /// <returns>The current instance of <see cref="ToastContentBuilder"/></returns> /// <exception cref="InvalidOperationException">Throws when attempting to add/reserve more than 4 lines on a single toast. </exception> /// <exception cref="ArgumentOutOfRangeException">Throws when <paramref name="hintMaxLines"/> value is larger than 2. </exception> /// <remarks>More info at: https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/adaptive-interactive-toasts#text-elements</remarks> public ToastContentBuilder AddText(string text, AdaptiveTextStyle?hintStyle = null, bool?hintWrap = default(bool?), int?hintMaxLines = default(int?), int?hintMinLines = default(int?), AdaptiveTextAlign?hintAlign = null, string language = default(string)) { int lineCount = GetCurrentTextLineCount(); if (GetCurrentTextLineCount() == 4) { // Reached maximum, we can't go further. throw new InvalidOperationException("We have reached max lines allowed (4) per toast"); } AdaptiveText adaptive = new AdaptiveText() { Text = text }; if (hintStyle != null) { adaptive.HintStyle = hintStyle.Value; } if (hintAlign != null) { adaptive.HintAlign = hintAlign.Value; } if (hintWrap != default(bool?)) { adaptive.HintWrap = hintWrap; } if (hintMaxLines != default(int?)) { if (hintMaxLines > 2) { throw new ArgumentOutOfRangeException(nameof(hintMaxLines), "max line can't go more than 2 lines."); } else if ((lineCount + hintMaxLines) > 4) { throw new InvalidOperationException($"Can't exceed more than 4 lines of text per toast. Current line count : {lineCount} | Requesting line count: {lineCount + hintMaxLines}"); } adaptive.HintMaxLines = hintMaxLines; } if (hintMinLines != default(int?) && hintMinLines > 0) { adaptive.HintMinLines = hintMinLines; } if (language != default(string)) { adaptive.Language = language; } return(AddVisualChild(adaptive)); }
/// <summary> /// Add a custom text that will appear on the tile notification. /// </summary> /// <param name="text">Custom text to display on the tile.</param> /// <param name="size">The tile size that the custom text would be added to. Default to all currently supported tile size.</param> /// <param name="hintStyle">Style that controls the text's font size, weight, and opacity.</param> /// <param name="hintWrap">Indicating whether text wrapping is enabled. For Tiles, this is false by default.</param> /// <param name="hintMaxLines">The maximum number of lines the text element is allowed to display. For Tiles, this is infinity by default</param> /// <param name="hintMinLines">The minimum number of lines the text element must display.</param> /// <param name="hintAlign">The horizontal alignment of the text</param> /// <param name="language"> /// 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. /// </param> /// <returns>The current instance of <see cref="TileContentBuilder"/></returns> public TileContentBuilder AddText(string text, TileSize size = AllSize, AdaptiveTextStyle?hintStyle = null, bool?hintWrap = default(bool?), int?hintMaxLines = default(int?), int?hintMinLines = default(int?), AdaptiveTextAlign?hintAlign = null, string language = default(string)) { // Create the adaptive text. AdaptiveText adaptive = new AdaptiveText() { Text = text }; if (hintStyle != null) { adaptive.HintStyle = hintStyle.Value; } if (hintAlign != null) { adaptive.HintAlign = hintAlign.Value; } if (hintWrap != default(bool?)) { adaptive.HintWrap = hintWrap; } if (hintMaxLines != default(int?)) { adaptive.HintMaxLines = hintMaxLines; } if (hintMinLines != default(int?) && hintMinLines > 0) { adaptive.HintMinLines = hintMinLines; } if (language != default(string)) { adaptive.Language = language; } // Add to the tile content return(AddAdaptiveTileVisualChild(adaptive, size)); }