Skip to content

Commit 9f504c9

Browse files
Added tags for code snippets.
1 parent d0f579f commit 9f504c9

File tree

1 file changed

+64
-17
lines changed

1 file changed

+64
-17
lines changed

MAUI/DataForm/AI-Powered-Smart-Paste-DataForm.md

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,28 @@ To enable AI functionality in your .NET MAUI Scheduler, first ensure that you ha
2828

2929
To connect your .NET MAUI app to Azure OpenAI, create a service class that handles communication with the AI model.
3030

31-
```
31+
{% tabs %}
32+
33+
{% highlight c# %}
34+
3235
/// <summary>
3336
/// Helper class to interact with Azure AI.
3437
/// </summary>
3538
internal class AzureOpenAIServiceConnector : AzureBaseService
3639
{
3740

3841
}
39-
```
42+
43+
{% endhighlight %}
44+
45+
{% endtabs %}
4046

4147
In this service, define a method called `GetAnswerFromGPT`. This method takes a user prompt from the SfAIAssistView control as input, sends it to the deployed model (e.g., GPT35Turbo), and returns the AI-generated response.
4248

43-
```
49+
{% tabs %}
50+
51+
{% highlight c# %}
52+
4453
/// <summary>
4554
/// Helper class to interact with Azure AI.
4655
/// </summary>
@@ -77,11 +86,17 @@ internal class AzureOpenAIServiceConnector : AzureBaseService
7786
return "";
7887
}
7988
}
80-
```
89+
90+
{% endhighlight %}
91+
92+
{% endtabs %}
8193

8294
Within the base service class (AzureBaseService), initialize the OpenAIClient with your Azure endpoint, deployment name, and API key.
8395

84-
```
96+
{% tabs %}
97+
98+
{% highlight c# %}
99+
85100
public abstract class AzureBaseService
86101
{
87102
#region Fields
@@ -139,15 +154,21 @@ Within the base service class (AzureBaseService), initialize the OpenAIClient wi
139154
}
140155
}
141156
}
142-
```
157+
158+
{% endhighlight %}
159+
160+
{% endtabs %}
143161

144162
## Integrating AI-powered Smart Paste in .NET MAUI DataForm
145163

146164
### Step 1: Create the DataForm Model
147165

148166
Define a model class (FeedBackForm) that represents the fields of the form. Add properties such as Name, Email, Product Name, Product Version, Rating and Comments. Use data annotations to configure display labels and validation rules.
149167

150-
```
168+
{% tabs %}
169+
170+
{% highlight c# %}
171+
151172
/// <summary>
152173
/// Feedback form model class
153174
/// </summary>
@@ -188,7 +209,10 @@ public class FeedBackForm
188209
[Display(ShortName = "Describe your feedback in detail", Name = "Comments")]
189210
public string Comments { get; set; }
190211
}
191-
```
212+
213+
{% endhighlight %}
214+
215+
{% endtabs %}
192216

193217
### Step 2: Bind the model to the dataform
194218

@@ -198,7 +222,10 @@ Create a view model containing an instance of the model. Assign this instance to
198222

199223
In XAML, set up the form layout - including labels, images, dataform control. Add a smart paste button that triggers that triggers the AI functionality and a submit button for data validation.
200224

201-
```
225+
{% tabs %}
226+
227+
{% highlight xaml %}
228+
202229
<Grid BackgroundColor="{DynamicResource SfDataFormNormalBackground}">
203230
<Image Source="{converters:SfImageResource feedbackform.png}" Aspect="Fill"/>
204231

@@ -331,7 +358,10 @@ In XAML, set up the form layout - including labels, images, dataform control. Ad
331358
</Border>
332359
</Grid>
333360
</Grid>
334-
```
361+
362+
{% endhighlight %}
363+
364+
{% endtabs %}
335365

336366
### Step 4: Implement Smart Paste Functionality.
337367

@@ -341,7 +371,10 @@ To implement this, the application first checks whether the clipboard contains a
341371

342372
Once the AI returns a response, the application deserialize the JSON string into a FeedBackForm object. The deserialized values are then assigned to the model bound to the DataForm, automatically updating the form fields with the extracted values.
343373

344-
```
374+
{% tabs %}
375+
376+
{% highlight c# %}
377+
345378
private async void OnlineSmartPasteButtonClicked(object? sender, EventArgs e)
346379
{
347380
if (Clipboard.Default.HasText)
@@ -399,9 +432,15 @@ Once the AI returns a response, the application deserialize the JSON string into
399432
this.UpdateOfflineSmartFillDataForm(finalResponse);
400433
}
401434
}
402-
```
403435

404-
```
436+
{% endhighlight %}
437+
438+
{% endtabs %}
439+
440+
{% tabs %}
441+
442+
{% highlight c# %}
443+
405444
private void UpdateOfflineSmartFillDataForm(string response)
406445
{
407446
//// Deserialize the JSON string to a Dictionary
@@ -430,14 +469,19 @@ Once the AI returns a response, the application deserialize the JSON string into
430469
this.dataForm!.UpdateEditor(filedNames[i]);
431470
}
432471
}
433-
}
434-
```
472+
473+
{% endhighlight %}
474+
475+
{% endtabs %}
435476

436477
### Step 5: Validate and Submit the form
437478

438479
Enable DataForm validation for all the fields during submission using the Validate method. If validation passes, display a confirmation message. If validation fails, show appropriate error messages.
439480

440-
```
481+
{% tabs %}
482+
483+
{% highlight c# %}
484+
441485
private void OnSubmitButtonClicked(object? sender, EventArgs e)
442486
{
443487
if (this.popup == null || this.dataForm == null)
@@ -456,7 +500,10 @@ private void OnSubmitButtonClicked(object? sender, EventArgs e)
456500

457501
this.popup.Show();
458502
}
459-
```
503+
504+
{% endhighlight %}
505+
506+
{% endtabs %}
460507

461508
![AI powered Smart Paste .NET MAUI Dataform](images/smart-ai-samples/ai-powered-smart-paste-.net-maui-dataform.png)
462509

0 commit comments

Comments
 (0)