mic*_*ver 9 c# winrt-xaml windows-store-apps
如何以编程方式将带换行符的文本添加到文本块?
如果我插入这样的文字:
helpBlock.Text = "Here is some text. <LineBreak/> Here is <LineBreak/> some <LineBreak/> more.";
Run Code Online (Sandbox Code Playgroud)
然后,换行符被解释为字符串文字的一部分.我希望它更像是如果我在XAML中拥有它会发生什么.
我似乎无法用WPF方式做到这一点:
helpBlock.Inlines.Add("Here is some content.");
Run Code Online (Sandbox Code Playgroud)
由于Add()方法想要接受"inline"类型的对象.
我无法创建一个Inline对象并将其作为参数传递,因为它由于其保护级别而"无法访问:
helpBlock.Inlines.Add(new Windows.UI.Xaml.Documents.Inline("More text"));
Run Code Online (Sandbox Code Playgroud)
我没有看到以编程方式添加运行的方法.
我可以找到大量的WPF示例,但WinRT没有.
我也发现了很多XAML示例,但C#中没有任何示例.
sa_*_*213 17
你可以直接传递换行符\n而不是<LineBreak/>
helpBlock.Text = "Here is some text. \n Here is \n some \n more.";
Run Code Online (Sandbox Code Playgroud)
或者在Xaml中,您将使用Hex换行符的值
<TextBlock Text="Here is some text. 
 Here is 
 some 
 more."/>
Run Code Online (Sandbox Code Playgroud)
两个结果:

使用Enviroment.NewLine
testText.Text = "Testing 123" + Environment.NewLine + "Testing ABC";
StringBuilder builder = new StringBuilder();
builder.Append(Environment.NewLine);
builder.Append("Test Text");
builder.Append(Environment.NewLine);
builder.Append("Test 2 Text");
testText.Text += builder.ToString();
Run Code Online (Sandbox Code Playgroud)