Ejemplo n.º 1
0
        // 尋找ID
        private bool SelectCardElementById(AdaptiveElement element, string id, out AdaptiveElement target)
        {
            bool isFound = false;

            if (element.Id != null && element.Id == id)
            {   // ID符合的物件
                target = element;
                return(true);
            }

            switch (element.Type)
            {   // 如果是Container
            case AdaptiveContainer.TypeName:
                isFound = SelectCardElementById((element as AdaptiveContainer).Items, id, out target);
                break;

            case AdaptiveColumnSet.TypeName:
                isFound = SelectCardElementById((element as AdaptiveColumnSet).Columns, id, out target);
                break;

            case AdaptiveColumn.TypeName:
                isFound = SelectCardElementById((element as AdaptiveColumn).Items, id, out target);
                break;

            case AdaptiveImageSet.TypeName:
                isFound = SelectCardElementById((element as AdaptiveImageSet).Images, id, out target);
                break;

            default:
                target = null;
                break;
            }
            return(isFound);
        }
Ejemplo n.º 2
0
        protected static void AddSeparator(HtmlTag uiContainer, AdaptiveElement adaptiveElement, AdaptiveRendererContext context)
        {
            if (!adaptiveElement.Separator && adaptiveElement.Spacing == AdaptiveSpacing.None)
            {
                return;
            }

            int spacing = context.Config.GetSpacing(adaptiveElement.Spacing);

            if (adaptiveElement.Separator)
            {
                SeparatorConfig sep   = context.Config.Separator;
                var             uiSep = new DivTag()
                                        .AddClass("ac-separator")
                                        .Style("padding-top", $"{spacing / 2}px")
                                        .Style("margin-top", $"{spacing / 2}px")
                                        .Style("border-top-color", $"{context.GetRGBColor(sep.LineColor)}")
                                        .Style("border-top-width", $"{sep.LineThickness}px")
                                        .Style("border-top-style", "solid");
                uiContainer.Children.Add(uiSep);
            }
            else
            {
                var uiSep = new DivTag()
                            .AddClass("ac-separator")
                            .Style("height", $"{spacing}px");
                uiContainer.Children.Add(uiSep);
            }
        }
        /// <summary>
        /// WeChat won't accept the image link outside its domain, recursive upload the image to get the url first.
        /// </summary>
        /// <param name="element">Adaptive card element.</param>
        /// <returns>Task of replace the adaptive card image uri.</returns>
        private async Task ReplaceAdaptiveImageUri(AdaptiveElement element)
        {
            if (element is AdaptiveImage adaptiveImage)
            {
                var attachmentData = await CreateAttachmentDataAsync(adaptiveImage.AltText ?? adaptiveImage.Id, adaptiveImage.Url.AbsoluteUri, adaptiveImage.Type).ConfigureAwait(false);

                var uploadResult = await _wechatClient.UploadNewsImageAsync(attachmentData).ConfigureAwait(false) as UploadPersistentMediaResult;

                adaptiveImage.Url = new Uri(uploadResult.Url);
                return;
            }

            if (element is AdaptiveImageSet imageSet)
            {
                foreach (var image in imageSet.Images)
                {
                    await ReplaceAdaptiveImageUri(image).ConfigureAwait(false);
                }
            }
            else if (element is AdaptiveContainer container)
            {
                foreach (var item in container.Items)
                {
                    await ReplaceAdaptiveImageUri(item).ConfigureAwait(false);
                }
            }
            else if (element is AdaptiveColumnSet columnSet)
            {
                foreach (var item in columnSet.Columns)
                {
                    await ReplaceAdaptiveImageUri(item).ConfigureAwait(false);
                }
            }
        }
        public static void AddSeparator(AdaptiveRenderContext context, AdaptiveElement element, Grid uiContainer)
        {
            if (element.Spacing == AdaptiveSpacing.None && !element.Separator)
            {
                return;
            }

            var uiSep = new Grid();

            uiSep.Style = context.GetStyle($"Adaptive.Separator");
            int spacing = context.Config.GetSpacing(element.Spacing);

            SeparatorConfig sepStyle = context.Config.Separator;

            uiSep.Margin = new Thickness(0, (spacing - sepStyle.LineThickness) / 2, 0, 0);
            uiSep.SetHeight(sepStyle.LineThickness);
            if (!string.IsNullOrWhiteSpace(sepStyle.LineColor))
            {
                uiSep.SetBackgroundColor(sepStyle.LineColor, context);
            }
            uiContainer.RowDefinitions.Add(new RowDefinition()
            {
                Height = GridLength.Auto
            });
            Grid.SetRow(uiSep, uiContainer.RowDefinitions.Count - 1);
            uiContainer.Children.Add(uiSep);
        }
Ejemplo n.º 5
0
        public static void AddSeperator(AdaptiveRenderContext context, AdaptiveElement element, Grid uiContainer)
        {
            if (element.Spacing == AdaptiveSpacing.None && !element.Separator)
            {
                return;
            }
            Grid grid = new Grid()
            {
                Style = context.GetStyle("Adaptive.Separator")
            };
            int             spacing   = context.Config.GetSpacing(element.Spacing);
            SeparatorConfig separator = context.Config.Separator;

            grid.Margin = new Thickness(0, (double)((spacing - separator.LineThickness) / 2), 0, 0);
            grid.SetHeight((double)separator.LineThickness);
            if (!string.IsNullOrWhiteSpace(separator.LineColor))
            {
                grid.SetBackgroundColor(separator.LineColor, context);
            }
            uiContainer.RowDefinitions.Add(new RowDefinition()
            {
                Height = GridLength.Auto
            });
            Grid.SetRow(grid, uiContainer.RowDefinitions.Count - 1);
            uiContainer.Children.Add(grid);
        }
Ejemplo n.º 6
0
 public static void ApplyIsVisible(FrameworkElement uiElement, AdaptiveElement element)
 {
     if (!element.IsVisible)
     {
         uiElement.Visibility = Visibility.Collapsed;
     }
 }
Ejemplo n.º 7
0
 // 尋找ID
 private bool SelectCardElementById <T>(List <T> elements, string id, out AdaptiveElement target)
 {
     foreach (var element in elements)
     {
         var isFound = SelectCardElementById(element as AdaptiveElement, id, out target);
         if (isFound)
         {
             return(isFound);
         }
     }
     target = null;
     return(false);
 }
Ejemplo n.º 8
0
        public static FrameworkElement ApplySelectAction(FrameworkElement uiElement, AdaptiveElement element, AdaptiveRenderContext context)
        {
            AdaptiveAction selectAction = null;

            if (element is AdaptiveCollectionElement)
            {
                selectAction = (element as AdaptiveCollectionElement).SelectAction;
            }
            else if (element is AdaptiveImage)
            {
                selectAction = (element as AdaptiveImage).SelectAction;
            }

            if (selectAction != null)
            {
                return(context.RenderSelectAction(selectAction, uiElement));
            }

            return(uiElement);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Get fallback text from the speech element
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string GetFallbackText(AdaptiveElement adaptiveElement)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            if (!string.IsNullOrEmpty(adaptiveElement.Speak))
            {
                var doc = new System.Xml.XmlDocument();
                var xml = adaptiveElement.Speak;
                if (!xml.Trim().StartsWith("<"))
                {
                    xml = $"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Speak>{xml}</Speak>";
                }
                else if (!xml.StartsWith("<?xml "))
                {
                    xml = $"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n{xml}";
                }
                doc.LoadXml(xml);
                return(doc.InnerText);
            }
#pragma warning restore CS0618 // Type or member is obsolete

            return(null);
        }
        /// <summary>
        /// Adds spacing as a grid element to the container
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="element">Element to render spacing for</param>
        /// <param name="uiContainer">Container of rendered elements</param>
        /// <returns>Spacing grid</returns>
        public static Grid AddSpacing(AdaptiveRenderContext context, AdaptiveElement element, Grid uiContainer)
        {
            if (element.Spacing == AdaptiveSpacing.None)
            {
                return(null);
            }

            var uiSpa = new Grid();

            uiSpa.Style = context.GetStyle($"Adaptive.Spacing");
            int spacing = context.Config.GetSpacing(element.Spacing);

            uiSpa.SetHeight(spacing);

            uiContainer.RowDefinitions.Add(new RowDefinition()
            {
                Height = GridLength.Auto
            });
            Grid.SetRow(uiSpa, uiContainer.RowDefinitions.Count - 1);
            uiContainer.Children.Add(uiSpa);

            return(uiSpa);
        }
Ejemplo n.º 11
0
        public virtual void Visit(AdaptiveElement adaptiveElement)
        {
            if (adaptiveElement is AdaptiveImage image)
            {
                Visit(image);
            }

            if (adaptiveElement is AdaptiveTextBlock textBlock)
            {
                Visit(textBlock);
            }

            // includes Column
            if (adaptiveElement is AdaptiveContainer container)
            {
                Visit(container);
            }

            if (adaptiveElement is AdaptiveColumnSet set)
            {
                Visit(set);
            }

            if (adaptiveElement is AdaptiveImageSet imageSet)
            {
                Visit(imageSet);
            }

            if (adaptiveElement is AdaptiveFactSet factSet)
            {
                Visit(factSet);
            }

            if (adaptiveElement is AdaptiveChoiceSetInput input)
            {
                Visit(input);
            }

            if (adaptiveElement is AdaptiveTextInput textInput)
            {
                Visit(textInput);
            }

            if (adaptiveElement is AdaptiveNumberInput numberInput)
            {
                Visit(numberInput);
            }

            if (adaptiveElement is AdaptiveDateInput dateInput)
            {
                Visit(dateInput);
            }

            if (adaptiveElement is AdaptiveTimeInput timeInput)
            {
                Visit(timeInput);
            }

            if (adaptiveElement is AdaptiveToggleInput toggleInput)
            {
                Visit(toggleInput);
            }

            if (adaptiveElement is AdaptiveMedia media)
            {
                Visit(media);
            }

            if (adaptiveElement is AdaptiveActionSet actionSet)
            {
                Visit(actionSet);
            }
        }
        public override AdaptiveCard GetPreviewCard()
        {
            string            broadcastType  = " ❗ EMERGENCY BROADCAST";
            AdaptiveTextColor broadcaseColor = AdaptiveTextColor.Attention;

            switch (Sensitivity)
            {
            case MessageSensitivity.Information:
                broadcastType  = " 📄 INFORMATION BROADCAST";
                broadcaseColor = AdaptiveTextColor.Good;
                break;

            case MessageSensitivity.Important:
                broadcastType  = " ❕ IMPORTANT BROADCAST";
                broadcaseColor = AdaptiveTextColor.Warning;
                break;

            default:
                break;
            }

            var previewCard = new AdaptiveCard(new AdaptiveSchemaVersion("1.0"))
            {
                Body = new List <AdaptiveElement>()
                {
                    new AdaptiveContainer()
                    {
                        Items = new List <AdaptiveElement>()
                        {
                            new AdaptiveTextBlock()
                            {
                                Id   = "sensitivity",
                                Text = broadcastType,
                                Wrap = true,
                                HorizontalAlignment = AdaptiveHorizontalAlignment.Left,
                                Spacing             = AdaptiveSpacing.None,
                                Weight   = AdaptiveTextWeight.Bolder,
                                Color    = broadcaseColor,
                                MaxLines = 1
                            }
                        }
                    },
                    new AdaptiveContainer()
                    {
                    },
                    new AdaptiveTextBlock()
                    {
                        Id     = "title",
                        Text   = Title, //  $"Giving Campaign 2018 is here",
                        Size   = AdaptiveTextSize.ExtraLarge,
                        Weight = AdaptiveTextWeight.Bolder,
                        Wrap   = true
                    },

                    new AdaptiveTextBlock()
                    {
                        Id       = "subTitle",
                        Text     = SubTitle, //  $"Have you contributed to Contoso's mission this year?",
                        Size     = AdaptiveTextSize.Medium,
                        Spacing  = AdaptiveSpacing.None,
                        Wrap     = true,
                        IsSubtle = true
                    },
                    new AdaptiveImage()
                    {
                        Id   = "bannerImage",
                        Url  = Uri.IsWellFormedUriString(ImageUrl, UriKind.Absolute)? new Uri(ImageUrl) : null,       //"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSG-vkjeuIlD-up_-VHCKgcREhFGp27lDErFkveBLQBoPZOHwMbjw"),
                        Size = AdaptiveImageSize.Stretch
                    },
                    new AdaptiveColumnSet()
                    {
                        Columns = new List <AdaptiveColumn>()
                        {
                            new AdaptiveColumn()
                            {
                                Width = AdaptiveColumnWidth.Auto,
                                Items = new List <AdaptiveElement>()
                                {
                                    // Need to fetch this from Graph API.
                                    new AdaptiveImage()
                                    {
                                        Id = "profileImage", Url = Uri.IsWellFormedUriString(Author?.ProfilePhoto, UriKind.Absolute)? new Uri(Author?.ProfilePhoto) : null, Size = AdaptiveImageSize.Small, Style = AdaptiveImageStyle.Person
                                    }                                                                                                                                                                                                                              //   new Uri("https://pbs.twimg.com/profile_images/3647943215/d7f12830b3c17a5a9e4afcc370e3a37e_400x400.jpeg"),Size=AdaptiveImageSize.Small,Style=AdaptiveImageStyle.Person
                                }
                            },
                            new AdaptiveColumn()
                            {
                                Width = AdaptiveColumnWidth.Auto,
                                Items = new List <AdaptiveElement>()
                                {
                                    new AdaptiveTextBlock()
                                    {
                                        Id     = "authorName",
                                        Text   = Author?.Name,        // "SERENA RIBEIRO",
                                        Weight = AdaptiveTextWeight.Bolder, Wrap = true
                                    },
                                    new AdaptiveTextBlock()
                                    {
                                        Id   = "authorRole",
                                        Text = Author?.Role,          //"Chief of Staff, Contoso Management",
                                        Size = AdaptiveTextSize.Small, Spacing = AdaptiveSpacing.None, IsSubtle = true, Wrap = true
                                    }
                                }
                            }
                        }
                    },
                    new AdaptiveContainer()
                    {
                        Items = new List <AdaptiveElement>()
                        {
                            new AdaptiveTextBlock()
                            {
                                Id   = "mainText",
                                Text = ShowAllDetailsButton ? Preview : Body,  // "The 2018 Employee Giving Campaign is officially underway!  Our incredibly generous culture of employee giving is unique to Contoso, and has a long history going back to our founder and his family’s core belief and value in philanthropy.   Individually and collectively, we can have an incredible impact no matter how we choose to give.  We are all very fortunate and 2018 has been a good year for the company which we are all participating in.  Having us live in a community with a strong social safety net benefits us all so lets reflect our participation in this year's success with participation in Give.",
                                Wrap = true
                            }
                        }
                    }
                },
            };

            // Image element without url does not render on phone. Remove empty images.
            AdaptiveElement adaptiveElement = previewCard.Body.FirstOrDefault(i => i.Id == "bannerImage");

            if (!Uri.IsWellFormedUriString(ImageUrl, UriKind.Absolute) && adaptiveElement != null)
            {
                previewCard.Body.Remove(adaptiveElement);
            }

            previewCard.Actions = new List <AdaptiveAction>();
            if (ShowAllDetailsButton)
            {
                previewCard.Actions.Add(new AdaptiveSubmitAction()
                {
                    Id    = "moreDetails",
                    Title = "More Details",
                    Data  = new AdaptiveCardValue <ActionDetails>()
                    {
                        Data = new AnnouncementActionDetails()
                        {
                            ActionType = Constants.ShowMoreDetails, Id = Id
                        }
                    }
                });

                if (IsAcknowledgementRequested)
                {
                    previewCard.Actions.Add(new AdaptiveSubmitAction()
                    {
                        Id    = "acknowledge",
                        Title = Constants.Acknowledge,
                        Data  = new AnnouncementActionDetails()
                        {
                            ActionType = Constants.Acknowledge, Id = Id
                        }
                    });
                }
                if (IsContactAllowed)
                {
                    previewCard.Actions.Add(new AdaptiveOpenUrlAction()
                    {
                        Id    = "contactSender",
                        Title = Constants.ContactSender,
                        Url   = new Uri($"https://teams.microsoft.com/l/chat/0/0?users={OwnerId}")
                    });
                }
            }

            return(previewCard);
        }
 public static void AddItemToAdaptiveColumn(AdaptiveColumn column, AdaptiveElement value)
 {
     column.Items.Add(value);
 }