2016-02-29 78 views
1

首先,我对XAML和C#都是全新的,所以请原谅我一些愚蠢的错误。一个解释是非常赞赏。[UWP] [C#]向RichTextBlock添加文本

我一直在努力尝试找出最简单的事情之一:如何在按下按钮时将文本添加到RichTextBlock。我尝试了一些我在互联网上找到的解决方案,但其中没有一个真正起作用。

特别是它似乎没有必要的参考。例如,VS不识别以下代码:Run myrun = new Run();,也不是Paragraph mypar = new Paragraph();myrichtextblock.document

我错过了什么?

回答

2

检查这个例子:

从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); 

就看看如何在这个例子中添加文本和修改代码把你的点击事件里面你按钮

请标记此答案,如果它对您有用!

最好的问候!

+0

正如我在我的帖子中说过的,我已经试过这段代码,它不工作,因为我之前提到过的行,比如'Run run = new Run();'因为Run似乎不存在本地范围。其他解决方案? – MasterArch

+0

请更新您的问题,例外 – RicardoPons

+0

我终于解决了它。我不只是包括Windows.UI.Xaml.Documents。非常感谢您的帮助! – MasterArch