-
Notifications
You must be signed in to change notification settings - Fork 5
Instructions 05 MAUI
Create a .NET MAUI application that can run on Windows, Android, iOS, and macOS. A library is created to send requests to the API service. This library can be used from all .NET applictions. A view-models library is created to offer the view-models used by the application. This library can be used by all XAML-based applications.
- Create a .NET MAUI application for multiple platforms.
- Add a reference to the NuGet packages Microsoft.Extensions.Http, Microsoft.Extensions.Configuration.Json
- Create two libraries for viewmodels and client-services libraries, and reference these as well: CodeBreaker.ViewModels and CodeBreaker.ClientServices
- With the client-services library, reference the shared library created before.
- Support both .NET 7 and .NET 8 with these libraries.
If you are not interested in developing the client library on your own, because you want to focus on MAUI or you simply have not enough time, you can use our NuGet package
CNinnovation.Codebreaker.GamesClient
.Bear in mind, that you still need to register the gamesclient in the DI container.
If you want to use our hosted backend you can define
https://codebreaker-gamesapi-3.purplebush-9a246700.westeurope.azurecontainerapps.io
asApiBase
in your configuration.
- Open the library CodeBreaker.ClientServices
- Reference the shared library created before, and add the NuGet package for logging, Microsoft.Extensions.Logging.Abstractions.
- Create the interfaces
IGameClient
- Create the GameClient implementation - inject the
HttpClient
and use it to call the REST API using extension methods such asGetFromJsonAsync
andPostAsJsonAsync
Build the project, create a NuGet package.
- Open the .NET MAUI application
- Add appsettings.json to the .NET MAUI application, and configure it as a MauiAsset, copy always
- Configure the AppBase setting with the configuration file
- Add the MAUI configuration to read the appsettings.json file
- Configure the HttpClient with MauiProgram.cs
public interface IGameClient
{
Task<CreateMoveResponse> SetMoveAsync(Guid gameId, params string[] colorNames);
Task<CreateGameResponse> StartGameAsync(string username);
}
Implement the GameClient
class, which implements both interfaces.
public class GameClient : IGameClient
{
private readonly HttpClient _httpClient;
private readonly ILogger _logger;
public GameClient(HttpClient httpClient, ILogger<GameClient> logger)
{
_httpClient = httpClient;
_logger = logger;
_logger.LogInformation("Injected HttpClient with base address {uri} into GameClient", _httpClient.BaseAddress);
}
public async Task<CreateGameResponse> StartGameAsync(string username)
{
CreateGameRequest request = new(username);
HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync("/games", request);
responseMessage.EnsureSuccessStatusCode();
return await responseMessage.Content.ReadFromJsonAsync<CreateGameResponse>();
}
public async Task<CreateMoveResponse> SetMoveAsync(Guid gameId, params string[] colorNames)
{
CreateMoveRequest request = new(colorNames.ToList());
HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync($"/games/{gameId}/moves", request);
responseMessage.EnsureSuccessStatusCode();
return await responseMessage.Content.ReadFromJsonAsync<CreateMoveResponse>();
}
}
Configure the HttpClient with the DI container and configuration
builder.Configuration.AddJsonStream(FileSystem.OpenAppPackageFileAsync("appsettings.json").Result);
builder.Configuration.AddJsonStream(FileSystem.OpenAppPackageFileAsync($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")}.json").Result);
//...
builder.Services.AddHttpClient<IGameClient, GameClient>(client =>
{
client.BaseAddress = new(builder.Configuration.GetRequired("ApiBase"));
});