Home > Software design >  MAUI: DependencyService.Get<IMyService>() return null
MAUI: DependencyService.Get<IMyService>() return null

Time:10-29

I'm trying to migrate from Xamarin to MAUI.

The following code woked perfectly.

Xamarin project:

DependencyService.Get<IMyService>().Test();

Android:

[assembly: Dependency(typeof(MyService))]
namespace MyProject.Android
{
    public class MyService: IMyService
    {
        public void Test(){ 
            ////
        }
    }
}

And so on and so forth for IOS and Windows.

But MAUI's DependencyService.Get always return null. I cannot find what new changes have to be made. Reworking all code based on DependencyService in my main project would be painful.

What changes should be made in that case?

CodePudding user response:

You don't have the concept of Platform dependency services in MAUI you can either use Conditional compilation or Partial classes to do what you need to do.

So if you check the Microsoft blog here: https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/invoke-platform-code it shows you how you can use both these ways to invoke platform code.

So get the Orientation through conditional compilation:

#if ANDROID
using Android.Content;
using Android.Views;
using Android.Runtime;
#elif IOS
using UIKit;
#endif

using InvokePlatformCodeDemos.Services;

namespace InvokePlatformCodeDemos.Services.ConditionalCompilation
{
    public class DeviceOrientationService
    {
        public DeviceOrientation GetOrientation()
        {
#if ANDROID
            IWindowManager windowManager = Android.App.Application.Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
            SurfaceOrientation orientation = windowManager.DefaultDisplay.Rotation;
            bool isLandscape = orientation == SurfaceOrientation.Rotation90 || orientation == SurfaceOrientation.Rotation270;
            return isLandscape ? DeviceOrientation.Landscape : DeviceOrientation.Portrait;
#elif IOS
            UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
            bool isPortrait = orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown;
            return isPortrait ? DeviceOrientation.Portrait : DeviceOrientation.Landscape;
#else
            return DeviceOrientation.Undefined;
#endif
        }
    }
}

Or Partial classes

Shared code:

namespace InvokePlatformCodeDemos.Services.PartialMethods
{
    public partial class DeviceOrientationService
    {
        public partial DeviceOrientation GetOrientation();
    }
}

Platform code:

using Android.Content;
using Android.Runtime;
using Android.Views;

namespace InvokePlatformCodeDemos.Services.PartialMethods;

public partial class DeviceOrientationService
{
    public partial DeviceOrientation GetOrientation()
    {
        IWindowManager windowManager = Android.App.Application.Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
        SurfaceOrientation orientation = windowManager.DefaultDisplay.Rotation;
        bool isLandscape = orientation == SurfaceOrientation.Rotation90 || orientation == SurfaceOrientation.Rotation270;
        return isLandscape ? DeviceOrientation.Landscape : DeviceOrientation.Portrait;
    }
}

Good luck!

  • Related