public void Update(AdaptiveBlockContent block, AdaptiveBlock sourceBlock, PreviewBlockHostViewModel args) { var viewModel = new PreviewEchoSpotViewModel(); var consumer = new AdaptiveBlockConsumer(AdaptiveBlock.Parse(sourceBlock.ToJson()).Block, new AdaptiveBlockConsumerProperties()); if (consumer.ConsumeContent(out var contentConsumer)) { if (contentConsumer.ConsumeText(out var text)) { viewModel.Title = text.Text; } if (contentConsumer.ConsumeText(out var textSubtitle)) { viewModel.Subtitle = textSubtitle.Text; } // In this case, if we only have a profile image, we'd rather use it as the background image // But if we have something to use as both the background and profile, then profile should be used. var backgroundImageRequest = AdaptiveBlockContentConsumer.ImageMatchRequest.ForBackgroundImage(); var profileOrIconImageRequest = new AdaptiveBlockContentConsumer.ImageMatchRequest() { RequiredHint = { AdaptiveBlockImageCategoryHints.Profile, AdaptiveBlockImageCategoryHints.Icon } }; contentConsumer.ConsumeImages(backgroundImageRequest, profileOrIconImageRequest); if (backgroundImageRequest.ImageResult != null) { viewModel.BackgroundImage = backgroundImageRequest.ImageResult.Url; } if (profileOrIconImageRequest.ImageResult != null) { viewModel.ProfileImage = profileOrIconImageRequest.ImageResult.Url; } } DataContext = viewModel; //DataContext = sourceBlock; }
protected override Task TransformAsync(AdaptiveBlock block, AdaptiveBlockTransformResult <ToastContent> result) { ToastContent content = new ToastContent() { Visual = new ToastVisual() { BindingGeneric = new ToastBindingGeneric() } }; var finalBlocks = block.GetFinalBlocks(); var firstBlock = finalBlocks.First(); var firstBlockContent = firstBlock.View?.Content; if (firstBlockContent != null) { foreach (var t in firstBlockContent.Text.Take(3)) { content.Visual.BindingGeneric.Children.Add(new AdaptiveText() { Text = t.Text }); } var profileOrIconImageRequest = new AdaptiveBlockContentConsumer.ImageMatchRequest() { RequiredHint = { AdaptiveBlockImageCategoryHints.Profile, AdaptiveBlockImageCategoryHints.Icon } }; var heroImageRequest = new AdaptiveBlockContentConsumer.ImageMatchRequest() { HintsToAvoid = { AdaptiveBlockImageCategoryHints.Icon } }; AdaptiveBlockContentConsumer.MatchImages(firstBlockContent, profileOrIconImageRequest, heroImageRequest); if (heroImageRequest.ImageResult != null) { content.Visual.BindingGeneric.HeroImage = new ToastGenericHeroImage() { Source = heroImageRequest.ImageResult.Url }; } if (profileOrIconImageRequest.ImageResult != null) { content.Visual.BindingGeneric.AppLogoOverride = new ToastGenericAppLogo() { Source = profileOrIconImageRequest.ImageResult.Url, HintCrop = profileOrIconImageRequest.ImageResult.Hints.Category.Contains(AdaptiveBlockImageCategoryHints.Profile) ? ToastGenericAppLogoCrop.Circle : ToastGenericAppLogoCrop.Default }; } if (finalBlocks.Count() > 1) { foreach (var extraBlock in finalBlocks.Skip(1)) { if (extraBlock.View?.Content != null) { content.Visual.BindingGeneric.Children.Add(CreateGroup(extraBlock.View.Content)); } } } if (block.View.Attributes?.AttributionText?.Text != null) { content.Visual.BindingGeneric.Attribution = new ToastGenericAttributionText() { Text = block.View.Attributes?.AttributionText?.Text }; } if (firstBlockContent.Actions.Any()) { content.Actions = new ToastActionsCustom(); var toastActions = content.Actions as ToastActionsCustom; foreach (var a in firstBlockContent.Actions) { // Quick reply style if (a is AdaptiveAction singleFinalAction && a.Inputs.Count == 1 && a.Inputs.First() is AdaptiveTextInputBlock singleTextInput) { toastActions.Inputs.Add(new ToastTextBox(singleTextInput.Id) { PlaceholderContent = singleTextInput.Placeholder }); var button = CreateButton(singleFinalAction); button.TextBoxId = singleTextInput.Id; toastActions.Buttons.Add(button); } else { foreach (var input in a.Inputs) { if (input is AdaptiveTextInputBlock textInput) { toastActions.Inputs.Add(new ToastTextBox(textInput.Id) { PlaceholderContent = textInput.Placeholder, Title = textInput.Title }); } else if (input is AdaptiveChoiceSetInputBlock choiceInput) { var selectionBox = new ToastSelectionBox(choiceInput.Id) { Title = choiceInput.Title }; foreach (var val in choiceInput.Choices) { selectionBox.Items.Add(new ToastSelectionBoxItem(val.Value, val.Title)); } toastActions.Inputs.Add(selectionBox); } } List <AdaptiveAction> finalActions; if (a is AdaptiveSharedInputActions sharedActions) { finalActions = sharedActions.Actions; } else if (a is AdaptiveAction finalAction) { finalActions = new List <AdaptiveAction>() { finalAction }; } else { finalActions = new List <AdaptiveAction>(); } foreach (var finalAction in finalActions) { toastActions.Buttons.Add(CreateButton(finalAction)); } } } }