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

 

Chapter 2 Working with Visual Studio 2022

•\

Use the Mac build host to compile and sign the iOS app using the

 

build tools.

•\

Invoke the Mac builds securely via SSH.

•\

If an iOS device is plugged into the Mac, Visual Studio 2022 is notified

 

of this, and the iOS toolbar will update accordingly.

For more information on enabling the Mac build host in Visual Studio using a network-connected Mac, see the following link on the Microsoft documentation site: https://docs.microsoft.com/en-us/dotnet/maui/ios/pair-to-mac.

As of the writing of this book, Visual Studio 2022 for Mac was still in preview. For more information on Visual Studio 2022 for Mac, see the following link: https://visualstudio.microsoft.com/vs/mac/preview/.

Visual Studio 2022 for Mac will bring fully native macOS UI applications built on

.NET 6, including native support for the Apple M1 chip to Mac developers.

Consuming REST Services in MAUI

Modern applications often use REST Web Services to provide some functionality to users. This can be anything from accessing data to providing cloud storage. When implementing this on a mobile device, you would need to determine if the device is connected to the Internet. If it is, then only can you consume the REST Web Service.

When writing any application, consuming a Web Service is quite straightforward. If this is your first time working with MAUI, you might be wondering how to accomplish this. MAUI will feel familiar if you’re used to working with ASP.NET Core Web Applications and dependency injection.

In this section, I will show you how to consume a Web Service to get the current weather and ten-day forecast for Los Angeles using the Weather API at www.weatherapi. com. You need to create an account to get an API Key to use in your REST calls. I will not go through exactly how to do this. If you do not want to use the Weather API, you can choose another suitable service to consume. The focus here is consuming a Web Service, not the type of service being consumed. The logic detailed here remains the same. Let’s have a look at the complete application first.

79

Chapter 2 Working with Visual Studio 2022

The Complete Weather App

When running the application for the first time, you will see an empty screen, as illustrated in Figure 2-20.

Figure 2-20.  Starting the Weather Forecast App

The reason for this is so that I can show you how to add a command to click the Refresh button.

After you click the Refresh button, the application will call the weather service API and return the data to the UI for display, as seen in Figure 2-21.

80

Chapter 2 Working with Visual Studio 2022

Figure 2-21.  The Refreshed Weather App

As seen in Figure 2-22, the layout of the MAUI solution is straightforward. When creating the solution, it will be created with the Resources and Platforms folders. The Resources folder, as the name suggests, will contain any assets required for the

application, such as images, icons, fonts, and so on. The Platforms folder contains the platform-specific code for Android, iOS, Mac Catalyst, Tizen, and Windows.

81

Chapter 2 Working with Visual Studio 2022

Figure 2-22.  The Weather App solution

The Target Platforms

It is also worth noting that you do not need to carry any dead weight in your project. If you are creating an application that only runs on Android, iOS, and Mac Catalyst, you can remove unnecessary platforms. Edit your csproj file by right-clicking the project in the Solution Explorer and selecting Edit Project File from the context menu. There you

82