dotnet / dotnet/maui-samples

Add a simple MAUI + Gemini chat sample

Open
#713 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

Hello, MAUI

I made a very small .NET MAUI app that shows how to connect to the Google Gemini API. The app has one page with a text box and a button. You type a question, it sends it to Gemini, and shows the answer.

The code is short and easy to follow. It uses:

`

private async Task CallGeminiAsync(string prompt)
{
string url = $"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={ApiKey}";

var request = new
{
contents = new[]
{
new {
parts = new[]
{
new { text = prompt }
}
}
}
};

var json = JsonSerializer.Serialize(request);

using var http = new HttpClient();
var response = await http.PostAsync(
url,
new StringContent(json, Encoding.UTF8, "application/json")
);

string jsonResponse = await response.Content.ReadAsStringAsync();

using var doc = JsonDocument.Parse(jsonResponse);
var text = doc.RootElement
.GetProperty("candidates")[0]
.GetProperty("content")
.GetProperty("parts")[0]
.GetProperty("text")
.GetString();

return text ?? "(empty response)";
}

`

Would you be open to adding this sample to the MAUI-Samples repo? I think it could help developers see how easy it is to connect MAUI apps with AI services.

Thanks.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.