Search code examples
c#jsonadaptive-cards

Referencing TargetElements when when authoring Adaptive Cards


I am authoring an Adaptive Card in C# and need to add an Image with a ToggleVisibilityAction. The JSON equivalent is:

{
  "type": "Image",
  "selectAction": {
    "type": "Action.ToggleVisibility",
    "title": "expand",
    "targetElements": [ "REFERENCE_1", "REFERENCE_2",]
  },
  "url": "MY_IMAGE_URL",
  "altText": "visible"
}

In the above REFERENCE_1 and REFERENCE_2 are the Id fields of the elements I want to target.

When authoring it in C#, I have

new AdaptiveImage()
{
  SelectAction = new AdaptiveToggleVisibilityAction()
  {
    Title = "collapse",
    TargetElements = REFERENCE_COLLECTION_HERE, 
  },
}

My challenge is that the JSON version accepts a string reference with an Id of the TargetElement but the C# version expects a List<AdaptiveTargetElement> where REFERENCE_COLLECTION_HERE is. How do I manage to reference the TargetElement while being able to add it where I want it in my card layout.


Solution

  • You can just use AdaptiveTargetElement objects instead of strings:

    new AdaptiveImage()
    {
        SelectAction = new AdaptiveToggleVisibilityAction()
        {
            Title = "collapse",
            TargetElements = new List<AdaptiveTargetElement>
            {
                new AdaptiveTargetElement("REFERENCE_1"),
                new AdaptiveTargetElement("REFERENCE_2"),
            },
        },
    }
    

    You can see the documentation for them here: https://adaptivecards.io/explorer/TargetElement.html