与换行符纲领性文本块入口 [英] programmatic textblock entry with linebreaks

查看:112
本文介绍了与换行符纲领性文本块入口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编程与换行符给TextBlock添加文字



如果我插入的文字是这样的:

  helpBlock.Text =下面是一些文字<断行/>此处为<断行/>有的<断行/>更多。 



然后换行符得到解释为字符串的一部分。我想这是更喜欢,如果我在XAML中有它,会发生什么



我似乎无法做到这一点的WPF的方式之一:

  helpBlock.Inlines.Add(下面是一些内容。); 



由于Add()方法,用以接受型内联的对象。

$



<$ p:乙
$ b

我不能因为它的保护级别创建一个内联对象,并将它作为参数传递,因为它是无法访问$ p> helpBlock.Inlines.Add(新Windows.UI.Xaml.Documents.Inline(更多文本));

我不明白的方式以编程方式添加运行。



我能找到一吨这样的例子WPF,但没有对WinRT的。



我也翻了很多的XAML的例子,但没有从C#。


解决方案

您可以只通过在新行 \\\
而不是<断行/>

  helpBlock.Text =这是一些文本。 \\\
下面是一些\\\
更\\\
;

或者在XAML中你会使用新行的六角

 < TextBlock的文本=这里是一些文本。 &安培;#X0A;下面是与放大器;#X0A;一些和放大器;#X0A;更多/> 



这两种结果:




How do I programmatically add text with line breaks to a textblock?

If I insert text like this:

helpBlock.Text = "Here is some text. <LineBreak/> Here is <LineBreak/> some <LineBreak/> more.";

Then the linebreaks get interpreted as part of the string literal. I want it to be more like what would happen if I had it in the XAML.

I can't seem to do it the WPF way either:

helpBlock.Inlines.Add("Here is some content.");

Since the Add() method wants to accept objects of type "inline".

I can't create an Inline object and pass it as a parameter because it is "inaccessible due to its protection level:

helpBlock.Inlines.Add(new Windows.UI.Xaml.Documents.Inline("More text"));

I don't see a way to programmatically add runs.

I can find a ton of WPF examples of this, but nothing for WinRT.

I've also turned up a lot of XAML examples, but nothing from C#.

解决方案

You could just pass in newline \n instead of <LineBreak/>

helpBlock.Text = "Here is some text. \n Here is \n some \n more.";

Or in Xaml you would use the Hex value of newline

 <TextBlock Text="Here is some text. &#x0a; Here is &#x0a; some &#x0a; more."/>

Both results:

这篇关于与换行符纲领性文本块入口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆