如何获得/检测的屏幕尺寸Xamarin.Forms? [英] How to get/detect screen size in Xamarin.Forms?

查看:342
本文介绍了如何获得/检测的屏幕尺寸Xamarin.Forms?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重写一个应用程序,我写的iOS。我会写一个Android版本,但认为这将会是更好地使这个使用Xamarin.Forms的机会。在同一时间做一个页面,现在我坚持一个网页,我需要在屏幕的宽度和高度上。有谁知道iOS的View.Frame.Width的Xamarin.Forms等价?

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?

推荐答案

目前还不能从Xamarin.Forms本身就是一种方式,但我们把它实现为PCL兼容接口Xamarin.Forms.Labs你可以从的NuGet获得或源$ C ​​$ C从GitHub上。

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有IDisplay财产的资料;高度,宽度,对于X - 安培像素密度; Y和两个扩展方法来计算大小的单位为英寸。

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:

<一个href="https://github.com/XForms/Xamarin-Forms-Labs/blob/master/samples/Xamarin.Forms.Labs.Sample/Pages/Services/ExtendedDeviceInfoPage.cs" rel="nofollow">https://github.com/XForms/Xamarin-Forms-Labs/blob/master/samples/Xamarin.Forms.Labs.Sample/Pages/Services/ExtendedDeviceInfoPage.cs

        #region Display information
        var display = device.Display;
        var displayFrame = new Frame();
        if (display != null)
        {
            displayFrame.Content = new StackLayout()
            {
                Children =
                {
                    new Label() { Text = display.ToString() },
                    new Label() { Text = string.Format("Screen width is\t {0:0.0} inches.", display.ScreenWidthInches()) },
                    new Label() { Text = string.Format("Screen height is\t {0:0.0} inches.", display.ScreenHeightInches()) },
                    new Label() { Text = string.Format("Screen diagonal size is\t {0:0.0} inches.", display.ScreenSizeInches()) }
                            }
                        };
        }
        else
        {
            displayFrame.Content = new Label() { TextColor = Color.Red, Text = "Device does not contain display information." };
        }

        stack.Children.Add(displayFrame); 
        #endregion

创建的显示属性在所有平台上的确切英寸通过英寸的框架,无论:

Creating an exact inch-by-inch frame on all platforms regardless of display properties:

<一个href="https://github.com/XForms/Xamarin-Forms-Labs/blob/master/samples/Xamarin.Forms.Labs.Sample/Pages/Services/AbsoluteLayoutWithDisplayInfoPage.cs" rel="nofollow">https://github.com/XForms/Xamarin-Forms-Labs/blob/master/samples/Xamarin.Forms.Labs.Sample/Pages/Services/AbsoluteLayoutWithDisplayInfoPage.cs

public class AbsoluteLayoutWithDisplayInfoPage : ContentPage
{
    public AbsoluteLayoutWithDisplayInfoPage(IDisplay display)
    {
        this.Title = "Absolute Layout With Display Info";
        var abs = new AbsoluteLayout();
        var inchX = display.WidthRequestInInches(1);
        var inchY = display.HeightRequestInInches(1);
        var originX = display.WidthRequestInInches(display.ScreenWidthInches() / 2);
        var originY = display.HeightRequestInInches(display.ScreenHeightInches() / 2);

        abs.Children.Add(new Label() { Text = "1\"x\"1\" blue frame" });

        abs.Children.Add(new Frame()
            {
                BackgroundColor = Color.Navy,
            },
            new Rectangle(originX - inchX/2, originY - inchY/2, inchX, inchY));

        abs.Children.Add(new Frame()
            {
                BackgroundColor = Color.White
            },
            new Rectangle(originX - inchX/16, originY - inchY/16, inchX/8, inchY/8));

        this.Content = abs;
    }
}

要到达设备信息或者设置DI解析器,或使用一个静态的容器。所有3平台与静态CurrentDevice特性的单设备拨打电话:

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:

resolverContainer.Register<IDevice>(t => WindowsPhoneDevice.CurrentDevice)
resolverContainer.Register<IDevice>(t => AppleDevice.CurrentDevice)
resolverContainer.Register<IDevice>(t => AndroidDevice.CurrentDevice)

这篇关于如何获得/检测的屏幕尺寸Xamarin.Forms?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆