Skip to content

Add a simple MAUI + Gemini chat sample #713

@UnderTheHoodRobin

Description

@UnderTheHoodRobin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions