在XAML for Windows Phone 8的文本块中编写C代码

时间:2013-11-29 20:34:17

标签: c# xaml windows-phone-8

我正在写一本以Windows Phone 8 App形式学习C语言的书。 我将在书中编写一些C代码示例。我正在文本块内写这一章的内容。以下是样本:

我写的代码是:

<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Let's start the programming by writing a small program which prints a message on the screen. The program would be like this:" VerticalAlignment="Top" Height="629" Width="410" FontSize="20" Margin="0,10,0,-22" Grid.ColumnSpan="2"/>

程序将是这样的:,我想编写一个格式为:

的C程序
#include<stdio.h>
void main()
{
   printf("Welcome to C programming");
}

如何在XAML中编写此内容,是否有任何方法可以在XAML中输入此格式化文本,并且显示所有格式。

2 个答案:

答案 0 :(得分:1)

您可以使用RichTextBox

示例:

<RichTextBox Name="richTB" IsReadOnly="True">
    <RichTextBox.Resources>
        <Style TargetType="{x:Type Paragraph}">
            <Setter Property="Margin" Value="0"/>
        </Style>
    </RichTextBox.Resources>
    <FlowDocument>
        <Paragraph>
            <Run Text="#include" Foreground="Gray" />
            <Run Text="&lt;stdio.h&gt;" Foreground="Red" />
        </Paragraph>
        <Paragraph>
            <Run Text="void " Foreground="Blue" />
            <Run Text="main()" Foreground="Black" />
        </Paragraph>
        <Paragraph>
            <Run>{</Run>
        </Paragraph>
        <Paragraph Margin="10,0,0,0">
            <TextBlock>
                 <Run Text="printf(" Foreground="Blue" />   
                 <Run Text='"Welcome to C programming"' Foreground="Red" />                           
                 <Run Text=");" Foreground="Black" />
            </TextBlock>                                                               
        </Paragraph>
        <Paragraph>
            <Run>}</Run>
        </Paragraph>
    </FlowDocument>
</RichTextBox>

答案 1 :(得分:0)

这可能就是你要找的东西

<StackPanel Background="White">
            <TextBlock>
                <Run Text="#include" Foreground="Gray" />
                <Run Text="&lt;stdio.h&gt;" Foreground="Red" />
            </TextBlock>
            <TextBlock>
                <Run Text="void " Foreground="Blue" />
                <Run Text="main()" Foreground="Black" />
            </TextBlock>
            <TextBlock Foreground="Black">{</TextBlock>
            <TextBlock>
             <Run Text="     printf(" Foreground="Blue" />   
             <Run Text='"Welcome to C programming"' Foreground="Brown" />                           
             <Run Text=");" Foreground="Blue" />
            </TextBlock>
            <TextBlock Foreground="Black">}</TextBlock>
        </StackPanel>