Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Approach to get auto-sized StackLayout measured height in DP #4329

Closed
sserdyuk opened this issue Jun 6, 2017 · 3 comments
Closed

Approach to get auto-sized StackLayout measured height in DP #4329

sserdyuk opened this issue Jun 6, 2017 · 3 comments
Labels

Comments

@sserdyuk
Copy link

sserdyuk commented Jun 6, 2017

I'm trying to figure out the height of a StackLayout element on the screen that has some content and is auto-sized. I need the sizing information to scroll and position elements on the screen. Whenever I call the effective* methods, I always get 0 in return. What's the proper approach to do these things?

@NickIliev
Copy link
Contributor

NickIliev commented Jun 7, 2017

To get the size of your auto-sized StackLayout you need to use getMeasuredHeight and getMeasuredWidth. These methods will return the value in screen pixels which means that you will have to divide the value by the current screen scale to receive the value in DP (a.k.a DIPs). Once you get your size you can scroll to the wanted position with the scrollToVerticalOffset method on your ScrollView (or scrollToHorizontalOffset offset if you need to scroll horizontally). You still need to wrap the measurement in small timeout to make sure that the StackLayout has all its children loaded and measured.
e.g.
page.ts

import { screen, ScreenMetrics } from "platform";
let screenScale = screen.mainScreen.scale;

export function onStackLoaded(args: EventData) {
    let stack = <StackLayout>args.object;

    setTimeout(function () {
        console.log("stack.getMeasuredHeight: " + stack.getMeasuredHeight());
        console.log("stack.getMeasuredWidth: " + stack.getMeasuredWidth());

        let heightDP = stack.getMeasuredHeight() / screenScale;
        let widthDP = stack.getMeasuredWidth() / screenScale;
        console.log("heightDP: " + heightDP);
        console.log("widthDP: " + widthDP);

        scroll.scrollToVerticalOffset(heightDP/2); // will scroll to 1000DP (as the total height is 2000)
    }, 100);
}

page.xml

<ScrollView id="scroll">
    <StackLayout id="stack" loaded="onStackLoaded" class="p-0 m-0" backgroundColor="gray" height="2000">
        <GridLayout width="200" height="200" backgroundColor="white"><Label text="1: 200 x 200"/></GridLayout>
        <GridLayout width="200" height="200" backgroundColor="blue"><Label text="2: 200 x 200"/></GridLayout>
        <GridLayout width="200" height="200" backgroundColor="white"><Label text="2: 200 x 200"/></GridLayout>
        <GridLayout width="200" height="200" backgroundColor="blue"><Label text="4: 200 x 200"/></GridLayout>
        <GridLayout width="200" height="200" backgroundColor="white"><Label text="5: 200 x 200"/></GridLayout>
        <GridLayout width="200" height="200" backgroundColor="blue"><Label text="6: 200 x 200"/></GridLayout>
        <GridLayout width="200" height="200" backgroundColor="white"><Label text="7: 200 x 200"/></GridLayout>
        <GridLayout width="200" height="200" backgroundColor="blue"><Label text="8: 200 x 200"/></GridLayout>
        <GridLayout width="200" height="200" backgroundColor="white"><Label text="9: 200 x 200"/></GridLayout>
        <GridLayout width="200" height="200" backgroundColor="blue" ><Label text="10: 200 x 200"/></GridLayout>
    </StackLayout>
</ScrollView>

Demonstration project can be found here

@NickIliev NickIliev changed the title Question: Why are effectiveHeight and other effective* properties always return 0? Approach to get auto-sized StackLayout measured height / width in DP Jun 7, 2017
@NickIliev NickIliev changed the title Approach to get auto-sized StackLayout measured height / width in DP Approach to get auto-sized StackLayout measured height in DP Jun 7, 2017
@RoyiNamir
Copy link

@NickIliev Is there any workaround for this setTimeout trick ? Ng event maybe ?

@lock
Copy link

lock bot commented Aug 27, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked and limited conversation to collaborators Aug 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants