-1

I'm very new to XAML and cannot seem to understand how to write code for a Textblock for which I can keep adding new lines of text as the rest of my code runs. What I basically want to do is write a simple app for Windows phone that does a simple task and every time it hits a response or an error, prints to this Textblock in the UI rather like a log or Debug.WriteLine or Console.WriteLine. Could someone please help me or at least tell me where I can learn how to do it myself? I've searched in vain as can be seen.

I've already tried Textblock.text += string and it doesn't seem to update at all.

2 Answers2

2

Using Uncode

        <TextBlock Text="Newline1 &#x0a; Newline2 &#x0a; Newline3"></TextBlock>

Using LineBreak

        <TextBlock Margin="0,20,0,0">Line1<LineBreak></LineBreak>Line2<LineBreak></LineBreak>Line3</TextBlock>

Using TextWrap property Link

 <TextBlock Margin="0,20,0,0" TextWrapping="Wrap" >
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </TextBlock>

Final xaml code

  <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <!--Using Uncode &#x0a; -->
    <TextBlock Text="Newline1 &#x0a; Newline2 &#x0a; Newline3"></TextBlock>

    <!--Using LineBreak Tag -->
    <TextBlock Margin="0,20,0,0">Line1<LineBreak></LineBreak>Line2<LineBreak></LineBreak>Line3</TextBlock>

   <!--Using TextWrap property  -->

    <TextBlock Margin="0,20,0,0" TextWrapping="Wrap" >
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </TextBlock>
</StackPanel>

Output

enter image description here

Using c#

In C#, what's the difference between \n and \r\n?

Community
  • 1
  • 1
Heena
  • 8,450
  • 1
  • 22
  • 40
1

Adding new lines to textblock in c# can be done like this:

tb.Text += "Hello" + Environment.NewLine + "Dude";

Using "+=" simply appends the string. Using "Environment.NewLine" gives you a linebreak.

Daniel Meixner
  • 1,829
  • 11
  • 10