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

Chapter 11 Getting Specific

Platform-Specific API Access

While .NET MAUI does provide you with a lot of functionality out of the box, there can be times when you need to write your own interaction with the platform-specific layer to achieve your goals. Whatever functionality can be achieved on a specific platform can also be achieved within a .NET MAUI application. You just might have to do the heavy lifting yourself. If your implementation is considered useful enough to other developers, you should propose the changes back to the .NET MAUI team.

There are two main concepts you can utilize when building platform-­ specific code in .NET MAUI. Let’s take a look at each one through the simple example of building a LocationService that returns the longitude and latitude of the headquarters for each platform provider (e.g., Google, Apple, and Microsoft).

Platform-Specific Code with Compiler Directives

You will most likely come across a usage of the #if compiler directive when working on a .NET MAUI application. I am not a big fan of them but I do accept that in some scenarios they do provide value.

namespace WidgetBoard.Services;

public class PlatformLocationService : ILocationService

{

public Task<Location> GetLocationAsync()

{

Location location;

#if ANDROID

location = new Location(37.419857, -122.078827); #elif WINDOWS

location = new Location(47.639722, -122.128333);

352

Chapter 11 Getting Specific

#else

location = new Location(37.334722, -122.008889);

#endif

return Task.FromResult(location);

}

}

The above code will be compiled in different ways based on the target platform. The resulting compiled code for the Android platform looks as follows:

namespace WidgetBoard.Services;

public class PlatformLocationService : ILocationService

{

public Task<Location> GetLocationAsync()

{

Location location;

location = new Location(37.419857, -122.078827);

return Task.FromResult(location);

}

}

This means that only the code specific to the platform will be compiled and shipped to that platform.

This approach can work well in this scenario, but as soon as you need to use multiple classes or other platform-specific libraries, the code will become complex very quickly. In more complex scenarios, you can use the platform-specific folders created in your project for you.

353

Chapter 11 Getting Specific

Platform-Specific Code in Platform Folders

I briefly covered these folders in Chapter 2. Each platform has a folder and the files inside each folder (e.g., /Platforms/Android/) will only be compiled for that platform when you are targeting it. In order to create the same PlatformLocationService from the previous section, you first need to create a partial class under the Services folder with the following contents:

namespace WidgetBoard.Services;

public partial class MultiPlatformLocationService : ILocationService

{

}

The above code will not compile now because you haven’t implemented ILocationService. This is expected until you add in your platform-specific implementations, so don’t worry. You add the partial keyword because this is only a partial implementation. The platform-­specific files and classes you will add shortly will complete this partial implementation.

Next, you need to create your Android platform-specific implementation. To do this, you add a new class file under the

/Platforms/Android/ folder and call it PlatformLocationService.cs, just like the one above. You want to modify its contents to the following:

namespace WidgetBoard.Services;

public partial class MultiPlatformLocationService

{

public Task<Location> GetLocationAsync()

{

return Task.FromResult(new Location(37.419857, -122.078827));

}

}

354