首先,我对 XAML 和 C# 都是全新的,所以请原谅我的一些愚蠢错误。非常感谢您的解释。
最近几个小时我一直在苦苦思索,试图找出最简单的事情之一:如何在按下按钮时向 RichTextBlock 添加文本。我尝试了一些在 Internet 上找到的解决方案,但没有一个真正有效。
特别是似乎没有必要的引用资料。例如 VS 无法识别以下代码:Run myrun = new Run();
或 Paragraph mypar = new Paragraph();
或 myrichtextblock.document
.
我错过了什么?
最佳答案
检查这个例子:
摘自 MSDN 文档
https://msdn.microsoft.com/library/windows/apps/br227565
// Create a RichTextBlock, a Paragraph and a Run.
RichTextBlock richTextBlock = new RichTextBlock();
Paragraph paragraph = new Paragraph();
Run run = new Run();
// Customize some properties on the RichTextBlock.
richTextBlock.IsTextSelectionEnabled = true;
richTextBlock.TextWrapping = TextWrapping.Wrap;
run.Text = "This is some sample text to show the wrapping behavior.";
richTextBlock.Width = 200;
// Add the Run to the Paragraph, the Paragraph to the RichTextBlock.
paragraph.Inlines.Add(run);
richTextBlock.Blocks.Add(paragraph);
// Add the RichTextBlock to the visual tree (assumes stackPanel is decalred in XAML).
stackPanel.Children.Add(richTextBlock);
看看如何在这个例子中添加文本并修改代码以放入按钮的点击事件中
如果对您有用,请标记此答案!
最好的问候!
关于paragraph - [UWP][C#] 向 RichTextBlock 添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35710355/