如何使用XAMLReader在控制器中创建文本块

时间:2015-10-07 03:15:02

标签: xamlreader

我想在控制器中使用XAML代码创建一个文本块。有可能吗?还有其他想法吗?

        // Create a string
        String sb = "<TextBlock Name='NameLabel' HorizontalAlignment='Left' TextWrapping='Wrap' FontSize='37.333' FontFamily='Intel Clear Light' Opacity='0.5' Width='277' Grid.Row='4' Grid.ColumnSpan='3' Grid.Column='1' Text='Name (as per IC)' Margin='1,0,0,80' Grid.RowSpan='2'/>";

        // Create a textblock using a XamlReader
        TextBlock myTextBlock = (TextBlock)XamlReader.Load(sb.ToString());

        // Add created button to previously created container.
        splMain.Children.Add(myTextBlock);

1 个答案:

答案 0 :(得分:0)

        I have change my code. And now I have an error of [ splMain.Children.Add(myTextBlock); Additional information: Object reference not set to an instance of an object.]
        // Create a string
        String sb = "<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Text=\"Name (as per IC)\" Margin=\"1,0,0,80\" Grid.RowSpan=\"2\"/>";

        // Create a textblock using a XamlReader
        TextBlock myTextBlock = (TextBlock)XamlReader.Load(sb.ToString());

        // Add created button to previously created container.
        splMain.Children.Add(myTextBlock);