Why does the grid only appear after I exit and re-enter the app?

QuakeulfQuakeulf NOMember ✭✭✭

So I have in my App.cs a Mainpage that is set to a tabbedpage which is set to a child contentpage which has a grid with two buttons as part of the stacklayout.

When I first open the app the buttons are not visible, but when I open it again they appear.

The grid is initialised in the C#-code of the child contentpage like this:

var thegrid = new Grid(); thegrid.Children.Add(new Button { Text = "1" }, 0, 0); thegrid.Children.Add(new Button { Text = "2" }, 1, 0);

And then later in the code used like this:

Content = new StackLayout { VerticalOptions = LayoutOptions.End, Padding = App.thepadding, Spacing = App.thespacing, Children = { thegrid, (other things here) }

What am I doing wrong? Thank you very much in advance for helping out.

Best Answer

Answers

  • seanydaseanyda GBMember ✭✭✭✭✭

    The App.MainPage Width and Height aren't updated until after the build has loaded. So on the initial build if you have been using Application.Current.MainPage.HeightRequest or WidthRequest the values will be incorrect or null until viewing the page again (because the values are updated). Best bet is to use the DependencyService to get the Width and Height or ditch it on the initial page and just use the values throughout the app.

  • QuakeulfQuakeulf NOMember ✭✭✭

    @lpdavies said:
    The App.MainPage Width and Height aren't updated until after the build has loaded. So on the initial build if you have been using Application.Current.MainPage.HeightRequest or WidthRequest the values will be incorrect or null until viewing the page again (because the values are updated). Best bet is to use the DependencyService to get the Width and Height or ditch it on the initial page and just use the values throughout the app.

    I have not used any width or height requests in the code through Application.Current.MainPage. I am only using the options you see in the code example for styling.

  • seanydaseanyda GBMember ✭✭✭✭✭

    @Quakeulf said:

    @lpdavies said:
    The App.MainPage Width and Height aren't updated until after the build has loaded. So on the initial build if you have been using Application.Current.MainPage.HeightRequest or WidthRequest the values will be incorrect or null until viewing the page again (because the values are updated). Best bet is to use the DependencyService to get the Width and Height or ditch it on the initial page and just use the values throughout the app.

    I have not used any width or height requests in the code through Application.Current.MainPage. I am only using the options you see in the code example for styling.

    But what are these?

    Padding = App.thepadding,
    Spacing = App.thespacing,
    

    From what i can see from the code you've provided. You must be doing some sort of calculation in the App.cs to calculate the Padding and Spacing, and chances are these are null and causing the problem.

  • QuakeulfQuakeulf NOMember ✭✭✭

    @lpdavies said:

    @Quakeulf said:

    @lpdavies said:
    The App.MainPage Width and Height aren't updated until after the build has loaded. So on the initial build if you have been using Application.Current.MainPage.HeightRequest or WidthRequest the values will be incorrect or null until viewing the page again (because the values are updated). Best bet is to use the DependencyService to get the Width and Height or ditch it on the initial page and just use the values throughout the app.

    I have not used any width or height requests in the code through Application.Current.MainPage. I am only using the options you see in the code example for styling.

    But what are these?

    Padding = App.thepadding,
    Spacing = App.thespacing,
    

    From what i can see from the code you've provided. You must be doing some sort of calculation in the App.cs to calculate the Padding and Spacing, and chances are these are null and causing the problem.

    These are declared like this:

    public static int thepadding = 32; public static int thespacing = 16;

    I've had to do this since working with global styles proved to be a lot more work than I had hoped it would be judging from the guides and documentation.

  • QuakeulfQuakeulf NOMember ✭✭✭

    Unfortunately it would seem that Xamarin's API-documentation is down now:

    https://developer.xamarin.com/api/property/Xamarin.Forms.Layout.Padding/

  • JohnHardmanJohnHardman GBUniversity mod

    @Quakeulf - You need to show more of the code for the page for people to answer "What am I doing wrong?" without guessing. For example, have you added RowDefinitions for the Grid?

  • NMackayNMackay GBInsider, University mod

    If you must put styling values in a class (and I don't recommend that approach) create your own static styles class rather than referencing the app class.

        public static class AppStyleValues
        {
        public static int thepadding = 32;
        public static int thespacing = 16;
    

    Seems all wrong though.

  • QuakeulfQuakeulf NOMember ✭✭✭

    @NMackay said:
    If you must put styling values in a class (and I don't recommend that approach) create your own static styles class rather than referencing the app class.

        public static class AppStyleValues
        {
      public static int thepadding = 32;
      public static int thespacing = 16;
    

    Seems all wrong though.

    I am aware it is wrong and I don't like doing this myself but trying public styles using Resources has so far not worked as intended and created a lot more work than I had hoped. Even without the class this approach works and is not the issue here. It seems @Ipdavies is correct with the loading of the grid.

  • QuakeulfQuakeulf NOMember ✭✭✭

    @JohnHardman said:
    @Quakeulf - You need to show more of the code for the page for people to answer "What am I doing wrong?" without guessing. For example, have you added RowDefinitions for the Grid?

    No, I have not.

  • QuakeulfQuakeulf NOMember ✭✭✭

    Yes, I am dumb. Thank you all for helping out! :3

Sign In or Register to comment.