简体   繁体   中英

UI disappearing after a few seconds

I've been building a Windows Phone app (8.1) that gets some JSON data, parses it, and then builds UI Elements as necessary. Getting and parsing work fine, and life is nice, but the UI Elements disappear after only a few seconds. An example can be seen here , where a user clicks a button and the Elements get created, but gets destro. Specifically this piece of code is how I'm implementing it.

private void doBuild(object sender, TappedRoutedEventArgs e) {
    Button myButton = new Button();
    myButton.Width = 160;
    myButton.Height = 72;
    myButton.Content = "Click Me";
    var margin = myButton.Margin;
    margin.Top = 250;
    margin.Left = 15;
    myButton.Margin = margin;
    LayoutRoot.Children.Add(myButton);
    LayoutRoot.UpdateLayout();
}

What am I doing wrong?

The problem is with your layout. You've set a width of 0% for the first column of your grid. Since your button is added per default to the first column, its width is set to 0.

Either change the size of your columns, or add the button to another column:

private void doBuild(object sender, TappedRoutedEventArgs e)
{
    Button myButton = new Button();
    myButton.Width = 160;
    myButton.Height = 72;
    myButton.Content = "Click Me";
    var margin = myButton.Margin;
    margin.Top = 250;
    margin.Left = 15;
    myButton.Margin = margin;

    Grid.SetColumn(myButton, 1);

    LayoutRoot.Children.Add(myButton);
    LayoutRoot.UpdateLayout();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM