Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lawrence_shaun_introducing_net_maui_build_and_deploy_crosspl.pdf
Скачиваний:
46
Добавлен:
26.06.2023
Размер:
5.15 Mб
Скачать

Chapter 10 Remote Data

{

get => temperature;

set => SetProperty(ref temperature, value);

}

public string Weather

{

get => weather;

set => SetProperty(ref weather, value);

}

That’s all you need in the view model for now. You can now register the widget and get it ready for your first test run.

Registering Your Widget

You first need to make use of a NuGet package in order to follow some recommended practices for the registration and usage of the HttpClient class. Go ahead and add the Microsoft.Extensions.Http NuGet package and then take a look at how to use it.

•\

Right-click the WidgetBoard solution.

•\

Select Manage NuGet Packages.

•\

Search for Microsoft.Extensions.Http.

•\

Select the correct package.

•\

Click Add Package.

Inside your MauiProgram.cs file you need to add the following lines into the CreateMauiApp method:

builder.Services.AddHttpClient<WeatherForecastService>();

317

Chapter 10 Remote Data

builder.Services.AddSingleton<IWeatherForecastService,

WeatherForecastService>();

WidgetFactory.RegisterWidget<WeatherWidgetView, WeatherWidgetVi ewModel>(WeatherWidgetViewModel.DisplayName); builder.Services.AddTransient<WeatherWidgetView>(); builder.Services.AddTransient<WeatherWidgetViewModel>();

The above code registers your widget’s view and view models with the dependency injection layer and also registers it with your WidgetFactory, meaning it can be created from your add widget overlay.

Testing Your Widget

If you run your application and add a weather widget, you can see the result in Figure 10-1.

Figure 10-1.  Application running and showing your weather widget rendering correctly

318