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 GetDeviceModel()
{
return Build.Model;
}
}
}

IOS:

public class DeviceInfoService : IDeviceInfoService
{
public string GetDeviceModel()
{
return UIKit.UIDevice.CurrentDevice.Model;
}
}

Step 3:

Now inject the interface “IDeviceInfoService” in the class where you want to access the platform code. As shown below I injected the constructor of MainPageViewModel

namespace MAUISample.ViewModels
{
public class MainPageViewModel
{
private readonly IDeviceInfoService _deviceInfoService;
public MainPageViewModel(IDeviceInfoService deviceInfoService)
{
_deviceInfoService= deviceInfoService;
}
}
}

Step 4:

Call the method as shown below

namespace MAUISample.VieWModels
{
public class MainPageViewModel
{
private readonly IDeviceInfoService _deviceInfoService;
public MainPageViewModel(IDeviceInfoService deviceInfoService)
{
_deviceInfoService= deviceInfoService;
}

public string DeviceModel { get; set; }

private void GetDeviceModel()
{
DeviceModel=_deviceInfoService.GetDeviceModel();
}

}
}

Using Partial Classes and Partial Methods

Partial Class:

A partial class allows a single class to be divided into two separate physical files. During compile time these files get compiled into a single class. A partial class can be created by adding a partial keyword to the class

Partial Method:

A Partial Method contains the declaration part in one partial class and a definition part in another partial class or may contain both declaration and definition in the same partial class.

Let's Invoke platform specific code using partial classes and methods

Step 1:

Create a partial class with partial method declaration outside of the platform folder

namespace MAUISample
{
public partial class DeviceInfoPartialClass
{
public partial string GetDeviceInfo();
}
}

Step 2:

Create a partial class with partial method implementation at each platform level.

Android:

Make sure the namespace is the same for all partial classes created.

(ex: instead of namespace MAUISample.Platform.Andriod it should be namespace MAUISample) similar to IOS. Otherwise, your project will not have complied it throws an error saying no implementation found.

namespace MAUISample
{
public partial class DeviceInfoPartialClass
{
public partial string GetDeviceInfo()
{
return Build.Model;
}
}
}

IOS:

namespace MAUISample
{
public partial class DeviceInfoPartialClass
{
public partial string GetDeviceInfo()
{
return UIKit.UIDevice.CurrentDevice.Model;
}
}
}

Step 3:

Now finally call your method to get results

public string DeviceModelPartial { get; set; }
private void GetDeviceModelUsingPartialClassMethod()
{
DeviceModelPartial = new DeviceInfoPartialClass().GetDeviceInfo();
}

Reference

please go to Github for a code sample

Image of author using buy me a coffee logo

--

--