简体   繁体   English

WP8:以编程方式创建的文本块未显示在应用程序中?

[英]wp8: programmatically created textblock is not displayed in app?

Well, 好,

I have created textblock programmatically in c#. 我已经在C#中以编程方式创建了textblock but its not displayed in app. 但未在应用中显示。 Whats wrong ? 怎么了 ?

Here's my updated c# code: 这是我更新的C#代码:

            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. 您已将TextBlock添加到StackPanel但尚未将StackPanel添加到可视树。 Assuming you want to add it to the LayoutRoot , you can do this 假设您要将其添加到LayoutRoot ,可以执行此操作

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 . 根据您的情况,使用UserControl可能会更好。

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

StackPanel1.Children.Add(fileName);

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

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