Member-only story

Invoke Platform Specific Code in .NET MAUI

Swetha vennapusa
3 min readFeb 18, 2023

.Net MAUI is the evolution of Xamarin forms. In Xamarin forms we can invoke platform-specific code using Dependency Services but we all know that MAUI follows a single project approach to provide better performance and productivity. We can invoke Platform Specific Code by following the below 2 approaches

  • Using Dependency Injection
  • Using Partial Classes and Partial Methods

Using Dependency Injection

Dependency Injection is a form of Inversion of Control(IOC) in which an object receives other objects that it depends on at the run time.

Let's Invoke Platform specific code in .Net MAUI using Dependency Injection by taking an example that provides us device model in Andriod and IOS

Step1:

Create an Interface IDeviceInfoService outside of the platforms folder

namespace MAUISample
{
public interface IDeviceInfoService
{
string GetDeviceModel();

}
}

Step2:

Create a service at each platform level that inherits IDeviceInfoService

Android:

namespace MAUISample.Platforms.Android
{
public class DeviceInfoService : IDeviceInfoService
{
public string…

--

--

Responses (2)