Forum Xamarin.Forms

Why my view.X and view.Y are null?

Poli97Poli97 Member ✭✭
edited February 10 in Xamarin.Forms

I need to get the coordinates of views on my screen, but when I try to use the commands view.X and view.Y I always get that they are equals to 0.

Code example:

`public MainPage()
{
InitializeComponent();

        BoxView box = new BoxView
        {
            BackgroundColor = Color.Green,
        };
        AbsoluteLayout.SetLayoutBounds(box, new Rectangle(0.6, 0.6, 0.25, 0.25));
        AbsoluteLayout.SetLayoutFlags(box, AbsoluteLayoutFlags.All);

        Console.WriteLine($"box coordinates: {box.X} {box.Y}");  //BOTH EQUAL TO 0

        absoluteLayout.Children.Add(box);
    }`

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Those will not be set until your view is actually laid out.

  • Poli97Poli97 Member ✭✭

    @JoeManke said:
    Those will not be set until your view is actually laid out.

    What does it mean? How do I get a View coordinates then?

  • JarvanJarvan Member, Xamarin Team Xamurai

    Add the view to the layout, then print the position info. Check the code:

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
    
            Title = "Absolute Layout Exploration - Code";
            var layout = new AbsoluteLayout();
    
            var centerLabel = new Label
            {
                Text = "I'm centered on iPhone 4 but no other device.",
                LineBreakMode = LineBreakMode.WordWrap
            };
    
            AbsoluteLayout.SetLayoutBounds(centerLabel, new Rectangle(115, 159, 100, 100));
            // No need to set layout flags, absolute positioning is the default
    
            var bottomLabel = new Label { Text = "I'm bottom center on every device.", LineBreakMode = LineBreakMode.WordWrap };
            AbsoluteLayout.SetLayoutBounds(bottomLabel, new Rectangle(.5, 1, .5, .1));
            AbsoluteLayout.SetLayoutFlags(bottomLabel, AbsoluteLayoutFlags.All);
    
            var rightBox = new BoxView { Color = Color.Olive };
            AbsoluteLayout.SetLayoutBounds(rightBox, new Rectangle(1, .5, 25, 100));
            AbsoluteLayout.SetLayoutFlags(rightBox, AbsoluteLayoutFlags.PositionProportional);
    
            var leftBox = new BoxView { Color = Color.Red };
            AbsoluteLayout.SetLayoutBounds(leftBox, new Rectangle(0, .5, 25, 100));
            AbsoluteLayout.SetLayoutFlags(leftBox, AbsoluteLayoutFlags.PositionProportional);
    
            var topBox = new BoxView { Color = Color.Blue };
            AbsoluteLayout.SetLayoutBounds(topBox, new Rectangle(.5, 0, 100, 25));
            AbsoluteLayout.SetLayoutFlags(topBox, AbsoluteLayoutFlags.PositionProportional);
    
            layout.Children.Add(bottomLabel);
            layout.Children.Add(centerLabel);
            layout.Children.Add(rightBox);
            layout.Children.Add(leftBox);
            layout.Children.Add(topBox);
    
            Content = layout;
    
            Console.WriteLine($"box coordinates: {leftBox.X} {leftBox.Y}");
            Console.WriteLine($"box coordinates: {centerLabel.X} {centerLabel.Y}");
            Console.WriteLine($"box coordinates: {rightBox.X} {rightBox.Y}");
            Console.WriteLine($"box coordinates: {topBox.X} {topBox.Y}"); 
        }
    }
    
  • Poli97Poli97 Member ✭✭

    @Jarvan said:
    Add the view to the layout, then print the position info. Check the code:

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
    
            Title = "Absolute Layout Exploration - Code";
            var layout = new AbsoluteLayout();
    
            var centerLabel = new Label
            {
                Text = "I'm centered on iPhone 4 but no other device.",
                LineBreakMode = LineBreakMode.WordWrap
            };
    
            AbsoluteLayout.SetLayoutBounds(centerLabel, new Rectangle(115, 159, 100, 100));
            // No need to set layout flags, absolute positioning is the default
    
            var bottomLabel = new Label { Text = "I'm bottom center on every device.", LineBreakMode = LineBreakMode.WordWrap };
            AbsoluteLayout.SetLayoutBounds(bottomLabel, new Rectangle(.5, 1, .5, .1));
            AbsoluteLayout.SetLayoutFlags(bottomLabel, AbsoluteLayoutFlags.All);
    
            var rightBox = new BoxView { Color = Color.Olive };
            AbsoluteLayout.SetLayoutBounds(rightBox, new Rectangle(1, .5, 25, 100));
            AbsoluteLayout.SetLayoutFlags(rightBox, AbsoluteLayoutFlags.PositionProportional);
    
            var leftBox = new BoxView { Color = Color.Red };
            AbsoluteLayout.SetLayoutBounds(leftBox, new Rectangle(0, .5, 25, 100));
            AbsoluteLayout.SetLayoutFlags(leftBox, AbsoluteLayoutFlags.PositionProportional);
    
            var topBox = new BoxView { Color = Color.Blue };
            AbsoluteLayout.SetLayoutBounds(topBox, new Rectangle(.5, 0, 100, 25));
            AbsoluteLayout.SetLayoutFlags(topBox, AbsoluteLayoutFlags.PositionProportional);
    
            layout.Children.Add(bottomLabel);
            layout.Children.Add(centerLabel);
            layout.Children.Add(rightBox);
            layout.Children.Add(leftBox);
            layout.Children.Add(topBox);
    
            Content = layout;
    
            Console.WriteLine($"box coordinates: {leftBox.X} {leftBox.Y}");
            Console.WriteLine($"box coordinates: {centerLabel.X} {centerLabel.Y}");
            Console.WriteLine($"box coordinates: {rightBox.X} {rightBox.Y}");
            Console.WriteLine($"box coordinates: {topBox.X} {topBox.Y}"); 
        }
    }
    

    Even with your code it still doesn't work, it keep saying they are equal 0 I don't know why

  • Poli97Poli97 Member ✭✭

    @Jarvan said:
    Add the view to the layout, then print the position info. Check the code:

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
    
            Title = "Absolute Layout Exploration - Code";
            var layout = new AbsoluteLayout();
    
            var centerLabel = new Label
            {
                Text = "I'm centered on iPhone 4 but no other device.",
                LineBreakMode = LineBreakMode.WordWrap
            };
    
            AbsoluteLayout.SetLayoutBounds(centerLabel, new Rectangle(115, 159, 100, 100));
            // No need to set layout flags, absolute positioning is the default
    
            var bottomLabel = new Label { Text = "I'm bottom center on every device.", LineBreakMode = LineBreakMode.WordWrap };
            AbsoluteLayout.SetLayoutBounds(bottomLabel, new Rectangle(.5, 1, .5, .1));
            AbsoluteLayout.SetLayoutFlags(bottomLabel, AbsoluteLayoutFlags.All);
    
            var rightBox = new BoxView { Color = Color.Olive };
            AbsoluteLayout.SetLayoutBounds(rightBox, new Rectangle(1, .5, 25, 100));
            AbsoluteLayout.SetLayoutFlags(rightBox, AbsoluteLayoutFlags.PositionProportional);
    
            var leftBox = new BoxView { Color = Color.Red };
            AbsoluteLayout.SetLayoutBounds(leftBox, new Rectangle(0, .5, 25, 100));
            AbsoluteLayout.SetLayoutFlags(leftBox, AbsoluteLayoutFlags.PositionProportional);
    
            var topBox = new BoxView { Color = Color.Blue };
            AbsoluteLayout.SetLayoutBounds(topBox, new Rectangle(.5, 0, 100, 25));
            AbsoluteLayout.SetLayoutFlags(topBox, AbsoluteLayoutFlags.PositionProportional);
    
            layout.Children.Add(bottomLabel);
            layout.Children.Add(centerLabel);
            layout.Children.Add(rightBox);
            layout.Children.Add(leftBox);
            layout.Children.Add(topBox);
    
            Content = layout;
    
            Console.WriteLine($"box coordinates: {leftBox.X} {leftBox.Y}");
            Console.WriteLine($"box coordinates: {centerLabel.X} {centerLabel.Y}");
            Console.WriteLine($"box coordinates: {rightBox.X} {rightBox.Y}");
            Console.WriteLine($"box coordinates: {topBox.X} {topBox.Y}"); 
        }
    }
    

    Strange behaviour, I just find out that the problem is only for Android, if I use iOS emulator the coordinates are ok

  • JarvanJarvan Member, Xamarin Team Xamurai

    Strange behaviour, I just find out that the problem is only for Android, if I use iOS emulator the coordinates are ok

    I test the code on ios and it works well. On Android, the result is alwaly the default value which may be a potential value. You can report the issue on github.

Sign In or Register to comment.