Monodroid screen dimensions

孤街浪徒 提交于 2019-12-11 10:20:18

问题


I'm trying to reference WindowManger so I can get the default screen's dimensions but I can't seem to find reference to it. (this is API 8, froyo 2.2). I even tried:

dynamic wm = Android.App.Application.Context.GetSystemService(Android.Content.Context.WindowService);
Display display = wm.getDefaultDisplay();

But got an error indicating that Object doesn't respond to getDefaultDisplay.

Also I tried:

var wm = (IWindowManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.WindowService);
Display display = wm.DefaultDisplay;

But I get an invalid cast exception.

Saw this post, but I can't resolve WindowManager. Any ideas what's going on here?


回答1:


WindowManager is the IWindowManager interface. Instead of the cast, try using the .JavaCast<T>() extension method:

var wm = context.GetSystemService(Android.Content.Context.WindowService)
    .JavaCast<IWindowManager>();
var d  = wm.DefaultDisplay;

You can also check out the AccelerometerPlay sample.




回答2:


Another simpler way is

Display display = this.WindowManager.DefaultDisplay;

You can get the Width and Height properties from the display object.




回答3:


using Android.Runtime; //for JavaCast

var windowManager = Android.App.Application.Context.GetSystemService(Android.Content.Context.WindowService).JavaCast<IWindowManager>();


来源:https://stackoverflow.com/questions/13634835/monodroid-screen-dimensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!