microsoft / AdaptiveCards

A new way for developers to exchange card content in a common and consistent way.
https://adaptivecards.io
MIT License
1.76k stars 552 forks source link

[Rendering] UWP not visualize card actions #8080

Open guru98 opened 2 years ago

guru98 commented 2 years ago

Target Platforms

UWP

SDK Version

3.0.2

Application Name

UWP app

Problem Description

I developed an uwp chat client long time ago where i was using uwp 2.7 but now i want to use 3.0.2. Updating code i found that AdaptiveCardRender doen't render adaptive card actions while it render perfectly actions if you use actionsset To do some test i wrote a very simple test. here is the codebehind of mainpage:

using AdaptiveCards.ObjectModel.Uwp;
using AdaptiveCards.Rendering.Uwp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Data.Json;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace TestCardRendering
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private AdaptiveHostConfig hostfile = null;
        public MainPage()
        {
            this.InitializeComponent();

        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {

            if (CardContainer.Children.Count > 0)
            {
                CardContainer.Children.Clear();
            }
            AdaptiveCardRenderer renderer = new AdaptiveCardRenderer();
            renderer.HostConfig= hostfile;
            RenderedAdaptiveCard rac = null;

            JsonObject.TryParse(CardJson.Text, out JsonObject jsonObjectCard);
            if (jsonObjectCard != null)
            {
                rac = renderer.RenderAdaptiveCardFromJsonString(CardJson.Text);
                var err = rac.Errors;
                var war = rac.Warnings;
                rac.Action += Rac_Action;
            }
            if(rac != null && rac.FrameworkElement != null) 
            {
                CardContainer.Children.Add(rac.FrameworkElement);
            }
        }

        private async void Rac_Action(RenderedAdaptiveCard sender, AdaptiveActionEventArgs args)
        {
            if (args.Action is AdaptiveOpenUrlAction openUrlAction)
            {
                await Launcher.LaunchUriAsync(openUrlAction.Url);
            }

            else if (args.Action is AdaptiveShowCardAction showCardAction)
            {
                // This is only fired if, in HostConfig, you set the ShowCard ActionMode to Popup.
                // Otherwise, the renderer will automatically display the card inline without firing this event.
            }

            else if (args.Action is AdaptiveSubmitAction submitAction)
            {
                // Get the data and inputs
                string data = submitAction.DataJson.Stringify();
                string inputs = args.Inputs.AsJson().Stringify();

                // Process them as desired
            }
        }

        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {

            Uri uri = new Uri("ms-appx:///HostConfig/HostConfig.json");
            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
            string payload = await FileIO.ReadTextAsync(file);
            var newHostConfig = AdaptiveHostConfig.FromJsonString(payload).HostConfig;
            if (newHostConfig != null)
            {
                hostfile = newHostConfig;

            }    

        }
    }
}

and here the xaml of the same page:

<Page
    x:Class="TestCardRendering.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestCardRendering"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <TextBox x:Name="CardJson" HorizontalAlignment="Left" Margin="593,46,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="484" Height="338" RenderTransformOrigin="0.681,0.53"/>
        <Button Content="LoadCard" Margin="1096,46,0,0" VerticalAlignment="Top" Width="95" Click="Button_Click"/>
        <StackPanel x:Name="CardContainer" BorderThickness="2" BorderBrush="Black"  Margin="24,46,932,616"/>
        <Button Content="Load Host" Margin="1096,129,0,0" VerticalAlignment="Top" Width="95" Click="Button_Click_1"/>

    </Grid>
</Page>

To simulate the issue just copy a payload of a card generate from designer(simplest as possible) and try. Just the body content is rendered

Screenshots

No response

Card JSON

{
    "type": "AdaptiveCard",
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.3",
    "body": [
        {
            "type": "Container",
            "items": [
                {
                    "type": "TextBlock",
                    "text": "test",
                    "wrap": true
                }
            ]
        }
    ],
    "actions": [
        {
            "type": "Action.Submit",
            "title": "Action.Submit"
        }
    ],
    "id": "card1"
}

Sample Code Language

No response

Sample Code

No response

guru98 commented 2 years ago

After a lot of test i understand that the problem is related to the not correct handling of dark theme in the sample host files but also in default settings. By default the background is white (for both themes) but actions (buttons ) are follow the theme settings: this means white over white. Also let me higlight that there are a lot of new feature to manage renderer, parsers, split among actions and elements that aren't documented. Question: have you a hostconfig file that works with templates?