2016-02-29 66 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