-2

Using Xamarin Forms Visual Studio 2019.

I need to get the DPI of the device.

This post has a solution: How to get DPI device to PCL in Xamarin. Forms?

Implementing the solution gave problems:

When applying the answer above I get this error message:

The type or namespace name 'DependencyAttribute' could not be found (are you missing a using directive or an assembly reference?)

In your android implementation, add a new class:

[assembly: Dependency(typeof(DisplayInfo))]
namespace .....

--I solved this compiler problem by replacing with: using Xamarin.Essentials;

[assembly: Xamarin.Forms.Dependency(typeof(DisplayInfo))]

However when I compile and run the code, it falls over when I try consume this in the Xamarin Core Code:

int dpi = DependencyService.Get<IDisplayInfo>().GetDisplayDpi();

I get this runtime error on the above line:

System.NullReferenceException: Object reference not set to an instance of an object

Any ideas? I would have added this question as a comment on the original, but as a new user do not have the points to do this... If someone could drop a comment on the original question's solution pointing it to this URL that would be helpful too.

Alex
  • 878
  • 1
  • 10
  • 25
  • You need to use like this: int dpi = DependencyService.Get().GetDisplayDpi(); – Tuğçe Arar Jul 25 '19 at 11:44
  • have you followed the solution correctly? from what i have seen, it's: int dpi = DependencyService.Get().GetDisplayDpi(); you need to create an Interface in PCL, then implement it in the platforms. and that way, you are able to get the dpi – Ricardo Dias Morais Jul 25 '19 at 11:46
  • I did do this: DependencyService.Get().GetDisplayDpi(); – it seems Stackoverflow did not like the < tag inside a question! – Robert Bertora - KAMOHA TECH Jul 25 '19 at 12:32

1 Answers1

0

So the answer to my own question:

The solution original offered said do this:

In your android implementation, add a new class:

  [assembly: Dependency(typeof(DisplayInfo))]
  namespace YourAppNamespace.Droid
  {
     public class DisplayInfo : IDisplayInfo
     {
         public int GetDisplayWidth()
         {
             (int)Android.App.Application.Context.Resources.DisplayMetrics.WidthPixels;
         }

         public int GetDisplayHeight()
         {
            (int)Android.App.Application.Context.Resources.DisplayMetrics.HeightPixels;
        }

         public int GetDisplayDpi()
        {
             (int)Android.App.Application.Context.Resources.DisplayMetrics.DensityDpi;
        }
     }
 }

The problem I did not see anywhere in the Android code that initialised this class, and I was unsure how best to do this really.

So I followed guidance for implementing the interface directly in the Android MainActivity.cs file, and this worked.. The video I watched to help me on that is here:

https://www.youtube.com/watch?v=lgcnYDb6cRQ&t=19s

  • That's not a very useful answer. "I fixed it. Watch this video" doesn't tell me **what** really fixed the problem. – JRE Jul 25 '19 at 15:25