简体   繁体   中英

wp8: programmatically created textblock is not displayed in app?

Well,

I have created textblock programmatically in c#. but its not displayed in app. Whats wrong ?

Here's my updated c# code:

            double left = 0, top = 15, right = 0, bottom = 0;
            double left1 = 0, top1 = 12, right1 = 0, bottom1 = 12;
            TextBlock fileName = new TextBlock();
            fileName.Margin = new Thickness(left, top, right, bottom);
            fileName.FontSize = 30;
            fileName.Foreground = new SolidColorBrush(Colors.White);
            fileName.TextAlignment = TextAlignment.Center;
            fileName.Text = "hello";
            StackPanel content = new StackPanel();
            content.Margin = new Thickness(left1, top1, right1, bottom1);
            content.SetValue(Grid.RowProperty, 0);
            content.Children.Add(fileName);;

You've added the TextBlock to the StackPanel but you haven't added the StackPanel to the visual tree. Assuming you want to add it to the LayoutRoot , you can do this

LayoutRoot.Children.Add(content);

As a side note, is there a reason you're doing this programmatically? Depending on your situation, you might be better off using a UserControl .

您需要将其添加到诸如StackPanel类的控件中

StackPanel1.Children.Add(fileName);

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