private void getMyIPAddress()
{
String Address = ""。
this.Dispatcher.Invoke(()=>
{
this.RichTextBox_logs.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
});
while (true)
{
this.Dispatcher.Invoke(()=>
{
this.RichTextBox_logs.AppendText(Environment.NewLine)。
//this.RichTextBox_logs.ScrollToEnd();。
});
WebRequest request = WebRequest.Create("http://checkip.dyndns.com/"/span>);
try
{
使用(WebResponse response = request.GetResponse()
{
using (StreamReader stream = new StreamReader(response.GetResponseStream()) )
{
地址 = stream.ReadToEnd()。
}
int first = Address.IndexOf("Address: ") 9;
int last = Address.IndexOf("</body>") 。
if (CurrentAddressHolder != Address.Substring(first, last - first))
{
CurrentAddressHolder = Address.Substring(first, last - first);
this.Dispatcher.Invoke(()=>
{
this.textBox_ip.Text = CurrentAddressHolder;
});
}
this.Dispatcher.Invoke(() =>
{
this.RichTextBox_logs.AppendText("IP是"/span> CurrentAddressHolder)。
});
}
}
catch(Exception e)
{
this.Dispatcher.Invoke(()=>
{
this.RichTextBox_logs.AppendText(e.ToString())。
});
}
}
我是多執行緒的新手,我不確定它是否影響到新行的語法。
非常歡迎任何意見或代碼修訂/改進。謝謝。
uj5u.com熱心網友回復:
看來你的問題是行距問題。
解決方案一:
代替AppendText()方法,使用以下方法在RichTextBox中添加一個文本行:
public void AddText(RichTextBox rtb, string message, double spacing = 4)
{
var paragraph = new Paragraph() { LineHeight = spacing };
paragraph.Inlines.Add(new Run(message))。
rtb.Document.Blocks.Add(paragraph)。
}
使用這種方法,你將能夠通過設定LineHeight值來以編程方式改變段落間的行距。
當使用這種方法時,沒有必要添加Environment.NewLine。每個塊(段落)將自動被格式化為不同的行。
解決方案二:
嘗試改變段落的邊距(見下面的帖子https://stackoverflow.com/a/445897/6630084):
<RichTextBox x:Name="RichTextBox_logs" >/span>
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}"/span>>
< Setter Property="Margin" Value="0"/>
</Style>>
</RichTextBox.Resources>
...
<RichTextBox>/span> ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/307901.html
標籤:
下一篇:<p>我正在拼命地尋找一個影像(它位于應用程式安裝目錄的一個子目錄中)。 我正在使用MVVM和.Net3.5,找到影像路徑并在屬性中回傳一個字串、一個Uri或一個位圖并在xaml中使用它很

