I am trying to rewrite an app that I wrote for iOS. I was going to write an android version but thought It'd be better to make this the opportunity to use Xamarin.Forms. Doing it one page at a time, now I'm stuck on a page where I need to get the screen's width and height. Does anyone know the equivalent of iOS' View.Frame.Width in Xamarin.Forms?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
Updating for future developers trying to do this that either haven't looked or missed it. The Page class inherits from VisualElement, which now has two properties (which happen to be bindable for use in XAML):
in your page codebehind you would just do something like:
if you needed to know if it changes, use the SizeChanged event:
ref: Microsoft Docs
Recipe
Create a new Xamarin.Android application named ScreenSize. Edit Main.axml so that it contains two TextViews:
Edit Activity1.cs, change the code in OnCreate to the following:
![enter image description here][1] private int ConvertPixelsToDp(float pixelValue) { var dp = (int) ((pixelValue)/Resources.DisplayMetrics.Density); return dp; }
Run the application. Depending on the device, it will display the screen height and width. The following screen shot is from a Galaxy Nexus:
[image link] http://i.stack.imgur.com/TQhba.png
In your App file define a public static variable
You should initialise the variable both in IOS and Android before calling the App constructer. For IOS, in FinishedLaunching method
and For Android, in OnCreate function
There is an easy way to get the screen's width and height in
Xamarin.Forms
and access it globally from everywhere in your app. I'd do something like this:1. Create two public members in your App.cs:
2. Set the values in your
MainActivity.cs
(Android) or yourAppDelegate.cs
(iOS):Android:
iOS:
If you are using xamarin forms, then you can find width and height of current screen in portable class library(pcl) like below.
For Width you use this in pcl,
For height you can do this in pcl,
There isn't currently a way from Xamarin.Forms itself but we have it implemented as PCL compatible interface in Xamarin.Forms.Labs which you can get from NuGet or source code from GitHub.
https://github.com/XForms/Xamarin-Forms-Labs
IDevice has IDisplay property with the information; height, width, pixel density for X & Y and couple of extension methods to calculate sized in inches.
Sample page for getting information from the device:
https://github.com/XForms/Xamarin-Forms-Labs/blob/master/samples/Xamarin.Forms.Labs.Sample/Pages/Services/ExtendedDeviceInfoPage.cs
Creating an exact inch-by-inch frame on all platforms regardless of display properties:
https://github.com/XForms/Xamarin-Forms-Labs/blob/master/samples/Xamarin.Forms.Labs.Sample/Pages/Services/AbsoluteLayoutWithDisplayInfoPage.cs
To get to the device info either set your DI resolver or use a static container. All 3 platforms have a singleton device calls with static CurrentDevice property: