Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Define layout sizes in inches with IDisplay extension (code only)

$
0
0

I am not sure if there is built in functionality in Xamarin Forms for specifying layout dimensions in device independent way and the Device.OnPlatform method seemed very cumbersome and unpractical to call on every page so I created some methods on device abstraction that I started before X.F was announced. It will give you display information at runtime and is PCL compliant. This way you can render elements by specifying an exact size in inches. If this is a useful feature, I would ask those interested in helping it to test on different devices as I only have a few. I did test this on couple of Lumia WP phones with different screen sized but not with 1080p and I only on iPod Touch 5G and Samsung Galaxy S3.

Screenshots with 1x1" Frame with 1/8x1/8" frame inside it. Verified with measurement tape that the actual dimensions on screen were exact. Code can be found in the XForms.Toolkit repository.

Page source:

namespace XForms.Toolkit.Sample
{
    public class AbsoluteLayoutWithDisplayInfoPage : ContentPage
    {
        public AbsoluteLayoutWithDisplayInfoPage(IDisplay display)
        {
            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;
        }
    }
}

Lumia 1020 screenshot:

S3 screenshot:

iPod screenshot:


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>