13

改行を含むテキストをプログラムでテキストブロックに追加するにはどうすればよいですか?

次のようなテキストを挿入すると:

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

次に、改行は文字列リテラルの一部として解釈されます。XAML にある場合にどうなるかをもっと知りたいです。

私はWPFの方法でもそれを行うことができないようです:

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

Add() メソッドは「インライン」タイプのオブジェクトを受け入れたいためです。

「保護レベルが原因でアクセスできないため、インライン オブジェクトを作成してパラメーターとして渡すことはできません。

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

プログラムで実行を追加する方法がわかりません。

これに関する WPF の例はたくさんありますが、WinRT の例はありません。

また、多くの XAML の例を見つけましたが、C# のものはありません。

4

5 に答える 5

19

\n代わりに改行を渡すことができます<LineBreak/>

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

または Xaml ではHex、改行の値を使用します

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

両方の結果:

ここに画像の説明を入力

于 2013-03-23T02:36:35.370 に答える
9

Environment.NewLine を使用する

testText.Text = "Testing 123" + Environment.NewLine + "Testing ABC";

StringBuilder builder = new StringBuilder();
builder.Append(Environment.NewLine);
builder.Append("Test Text");
builder.Append(Environment.NewLine);
builder.Append("Test 2 Text");
testText.Text += builder.ToString();
于 2013-03-23T01:43:26.653 に答える
1

解決:

改行の代わりに「\n」を使用します。最善の方法は、次のように使用することです。

Resources.resx ファイル:

myTextline: "Here is some text. \n Here is \n some \n more."

あなたのクラスで:

helpBlock.Text = Resources.myTextline;

これは次のようになります。

ここに画像の説明を入力

他の解決策は、ここで Environment.NewLine を使用して文字列を作成することです。

StringBuilder builder = new StringBuilder();
builder.Append(Environment.NewLine);
builder.Append(Resources.line1);
builder.Append(Environment.NewLine);
builder.Append(Resources.line2);
helpBlock.Text += builder.ToString();

または、ここで「\n」を使用します

StringBuilder builder = new StringBuilder();
builder.Append("\n");
builder.Append(Resources.line1);
builder.Append("\n");
builder.Append(Resources.line2);
helpBlock.Text += builder.ToString();
于 2015-09-03T13:52:53.760 に答える
1

プログラムで変換でき\nます<LineBreak/>

    string text = "This is a line.\nThis is another line.";
    IList<string> lines = text.Split(new string[] { @"\n" }, StringSplitOptions.None);

    TextBlock tb = new TextBlock();
    foreach (string line in lines)
    {
        tb.Inlines.Add(line);
        tb.Inlines.Add(new LineBreak());
    }
于 2015-09-03T13:40:57.697 に答える