简体   繁体   English

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

[英]Write C code in Text Block in XAML for Windows Phone 8

I am writing a book for learning C language in the form of a Windows Phone 8 App. 我正在写一本以Windows Phone 8 App形式学习C语言的书。 I will be writing some C Code examples in the book. 我将在本书中编写一些C代码示例。 I am writing the content of the chapter inside a text block. 我在一个文本块内编写该章的内容。 Here is the sample: 这是示例:

The code that I have written is: 我写的代码是:

<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"/>

After the line The program would be like this: , i want to write a C program formatted as: 在该行之后, 程序将如下所示:我想编写一个格式为C的C程序:

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

How do I go about writing this in XAML, is there any way by which I enter this formatted text in XAML and it appears with all the formatting. 如何使用XAML编写代码,是否可以通过任何方式在XAML中输入此格式的文本,并且该文本与所有格式一起出现。

You can use RichTextBox . 您可以使用RichTextBox

Example: 例:

<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>

This may be what you are looking for 这可能是您要寻找的

<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>

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

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