0
私はWPFにはまったく新しいものですので、私の知らない言い訳をしてください。私はこの問題を約30分でグーグルグーグルで試してみましたが、私のシナリオに合った簡単な解決策は見つけられないようです。WPF RichTextBoxのコードを効率よくコード化する
基本的には、サーバーから回線を読み取るIRCアプリケーションを作成して、ニックネームとメッセージを読みました。私は太字で<Nick>
一部をしたい
<Nick> Message typed...
:とのような行を置きます。後で多分色で。また、各ループでいくつかの例を見ましたが、1000 +ラインがあり、毎回foreachループを実行すると、誰かが何かを入力すると非効率ではありませんか?
C#プログラミング側から、最善の/最も簡単なアプローチをお勧めします。
private void OnChannelMessage(object sender, IrcEventArgs e)
{
if (!txtActivity.Dispatcher.CheckAccess())
{
txtActivity.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
{
txtActivity.AppendText(string.Format("<Bold><{0}:{1}></Bold> {2}\n", e.Data.Channel, e.Data.Nick, e.Data.Message));
//Paragraph p = new Paragraph();
//p.Inlines.Add(new Bold(new Run(string.Format("<{0}> ", e.Data.Nick))));
//p.Inlines.Add(new Run(string.Format("{0}\n", e.Data.Message)));
//txtActivity.Document.Blocks.Add(p); // problem adds a big space between lines.
}
));
}
}
XAML:
<Grid>
<RichTextBox Name="txtActivity" VerticalScrollBarVisibility="Auto" Margin="12,12,12,41" BorderThickness="2" FontFamily="Consolas">
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
</RichTextBox>
<Button Content="Quit" Height="23" HorizontalAlignment="Left" Margin="628,371,0,0" Name="btnQuit" VerticalAlignment="Top" Width="50" Click="btnQuit_Click" />
</Grid>