You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now that we know how to present basic markdown content, let's try displaying something more elaborate by leveraging the power of [Adaptive Cards](https://adaptivecards.io/). This is useful for creating forms, or for displaying more complex content.
14
+
Now that we know how to present basic markdown content, let's try displaying something more elaborate by leveraging the power of **[Adaptive Cards](https://adaptivecards.io/)**. This is useful for creating forms, or for displaying more complex content.
15
15
16
16
## Working with forms
17
17
18
-
Command Palette supports Adaptive Cards, which are a way to create rich, interactive content. This can be useful for creating forms, or for displaying more complex content.
18
+
You can create a card in the Command Palette with the `IFormContent` interface (see [FormContent](./microsoft-commandpalette-extensions-toolkit/formcontent.md) for the toolkit implementation). This allows you to provide the Adaptive Card JSON, and the Command Palette will render it for you. When the user submits the form, Command Palette will call the `SubmitForm` method on your form, with the JSON payload and inputs from the form.
19
19
20
-
You can create a card in the Command Palette with the **IFormContent** interface (see [FormContent](./microsoft-commandpalette-extensions-toolkit/formcontent.md) for the toolkit implementation). This allows you to provide the Adaptive Card JSON, and the Command Palette will render it for you. When the user submits the form, Command Palette will call the **SubmitForm** method on your form, with the JSON payload and inputs from the form.
20
+
> [!TIP]
21
+
> Adaptive card payloads can be created using the [Adaptive Card Designer](https://adaptivecards.io/designer/). You can design your card there, and then copy the JSON payload into your extension.
21
22
22
-
Adaptive card payloads can be created using the [Adaptive Card Designer](https://adaptivecards.io/designer/). You can design your card there, and then copy the JSON payload into your extension.
23
+
1. In the `Pages` directory, add a new class
24
+
1. Name the class `FormPage`
25
+
1. Update the `FormPage` class:
23
26
24
-
For a full example of using Forms and Content pages, head on over to [`SamplePagesExtension/Pages/SampleContentPage.cs`](https://github.com/microsoft/PowerToys/blob/main/src/modules/cmdpal/ext/SamplePagesExtension/Pages/SampleContentPage.cs). Some brief things to note:
The FormPage is a content page that displays a form (`SampleContentForm`) to the user. It creates an instance of `SampleContentForm`, which is a form (defined later) that describes the UI and logic for a user input form.
44
+
45
+
1. At the bottom of file (under the `FormPage` class) add:
Debug.WriteLine($"Form submitted with formInput: {formInput}");
95
+
if (formInput==null)
96
+
{
97
+
returnCommandResult.GoHome();
98
+
}
99
+
ConfirmationArgsconfirmArgs=new()
100
+
{
101
+
PrimaryCommand=newAnonymousCommand(
102
+
() =>
103
+
{
104
+
string?name=formInput["Name"]?.ToString();
105
+
ToastStatusMessaget=new($"Hi {name}"??"No name entered");
106
+
t.Show();
107
+
})
108
+
{
109
+
Name="Confirm",
110
+
Result=CommandResult.KeepOpen(),
111
+
},
112
+
returnCommandResult.Confirm(confirmArgs);
113
+
}
114
+
}
115
+
```
116
+
117
+
The `SampleContentForm` contains the form and form submission logic. The `TemplateJson` contains the form structure and actions. This example only contains one text input which has the id of "Name" and has one action of submitting the form. The `SubmitForm` handles parsing thepayload; if its invalid will return the command to home and otherwise will display a confirmation dialog and a toast notification.
AdaptiveCardscandomorecomplexforms, includingusinganotherjsonobjecttodynamicallycreatecustomforms. You'llfirstsetupyourformwiththe [AdaptiveCardDesigner](https://adaptivecards.io/designer/) and then update your command.
- Defineyourformlayoutusingthe `TemplateJson` propertyofyour `FormContent`. ThisistheJSONpayloadfromtheCARDPAYLOADEDITORinthehttps://adaptivecards.io/designer/. It describes the structure and UI of your form.
- Set the **TemplateJson** property of your form to the JSON payload of your Adaptive Card. (this is the "CARD PAYLOAD EDITOR" value in the Adaptive Card Designer)
27
-
- Set the **DataJson** property of your **FormContent** to the data you want to use to fill in your card template. (This is the "SAMPLE DATA EDITOR" value in the Adaptive Card Designer). This is optional, but can make authoring cards easier.
28
-
- Implement the **SubmitForm** method to handle the form submission. This method will be called when the user submits the form, and will be passed the JSON payload of the form.
@@ -46,7 +293,8 @@ public override CommandResult SubmitForm(string payload)
46
293
}
47
294
```
48
295
49
-
Of course, you can mix and match **IContent** in whatever way you'd like. For example, you could use a block markdown first for the body of a post, and have a **FormContent** next to reply to the post.
296
+
> [!NOTE]
297
+
> You can mix and match different `IContent` types in your extension. For example, you might use a `MarkdownContent` block to display a post, followed by a `FormContent` block to collect a reply.
0 commit comments