简体   繁体   English

几秒钟后,UI消失了

[英]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. 我一直在构建Windows Phone应用程序(8.1),该应用程序获取一些JSON数据,进行解析,然后根据需要构建UI元素。 Getting and parsing work fine, and life is nice, but the UI Elements disappear after only a few seconds. 获取和解析工作正常,生活也很好,但是UI元素仅在几秒钟后消失。 An example can be seen here , where a user clicks a button and the Elements get created, but gets destro. 可以在此处看到一个示例,其中用户单击按钮,然后创建Elements,但是破坏了。 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. 您已经为表格的第一列设置了0%的宽度。 Since your button is added per default to the first column, its width is set to 0. 由于默认情况下将按钮添加到第一列,因此其宽度设置为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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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