【问题标题】:[UWP][C#] Adding text to RichTextBlock[UWP][C#] 向 RichTextBlock 添加文本
【发布时间】:2016-02-29 21:47:55
【问题描述】:

首先,我对 XAML 和 C# 完全陌生,所以请原谅我的一些愚蠢错误。更感谢您的解释。

在过去的几个小时里,我一直在努力找出最简单的事情之一:当按下按钮时如何将文本添加到 RichTextBlock。我尝试了一些在 Internet 上找到的解决方案,但没有一个真正奏效。

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

我错过了什么?

【问题讨论】:

    标签: paragraph richtextblock


    【解决方案1】:

    检查这个例子:

    取自 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);
    

    看看如何在这个例子中添加文本并修改代码以放入按钮的点击事件中

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

    最好的问候!

    【讨论】:

    • 正如我在帖子中所说,我已经尝试过这段代码,但由于我之前提到的行,它不起作用,例如Run run = new Run();,因为本地似乎不存在 Run范围。还有其他解决方案吗?
    • 我终于解决了。我不只是简单地包括 Windows.UI.Xaml.Documents。非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多