语句执行但不将文本附加到 TextBox
Statements execute but do not append text to the TextBox
我已经创建了一个 C# WPF 应用程序。它从文本框中获取 URL 输入,并在单击按钮时在另一个文本框中显示下载的 html。为了告诉用户等到网页下载完成,我在开头附加了文字。
public void urlAnalyzer()
{
// Append text to Result box
Result.AppendText("Please wait, inspecting the URL.\n");
// Initiating WebClient to download webpage
WebClient inspecter = new WebClient();
// try-catch to avoid exception in a generic way
try
{
// stroring downloaded page in savedData
savedData = inspecter.DownloadString(webpage);
// appending downloaded html in Result box
Result.AppendText(savedData);
}
catch
{
Result.AppendText("You did not enter any valid URL.");
}
}
调用urlAnalyzer()
时,Result.AppendText()
转到结果文本框的事件处理方法,
private void Result_TextChanged(object sender, TextChangedEventArgs e)
{
}
每次调用 Result.AppendText()
时都会访问此事件方法,但不会将字符串附加到结果框中。当 urlAnalyzer()
函数被完全访问时,文本将出现在结果框中。
如何在执行追加语句时使追加的文本出现在文本框中?如何在每次文本追加调用时更新文本框?
WebClient.DownloadString()
在 UI 线程中执行并阻止 UI 更新。使用异步版本的下载方法:
public async void urlAnalyzer()
{
// Append text to Result box
Result.AppendText("Please wait, inspecting the URL.\n");
// Initiating WebClient to download webpage
WebClient inspecter = new WebClient();
// try-catch to avoid exception in a generic way
try
{
// stroring downloaded page in savedData
savedData = await inspecter.DownloadStringTaskAsync(webpage);
// appending downloaded html in Result box
Result.AppendText(savedData);
}
catch
{
Result.AppendText("You did not enter any valid URL.");
}
}
我已经创建了一个 C# WPF 应用程序。它从文本框中获取 URL 输入,并在单击按钮时在另一个文本框中显示下载的 html。为了告诉用户等到网页下载完成,我在开头附加了文字。
public void urlAnalyzer()
{
// Append text to Result box
Result.AppendText("Please wait, inspecting the URL.\n");
// Initiating WebClient to download webpage
WebClient inspecter = new WebClient();
// try-catch to avoid exception in a generic way
try
{
// stroring downloaded page in savedData
savedData = inspecter.DownloadString(webpage);
// appending downloaded html in Result box
Result.AppendText(savedData);
}
catch
{
Result.AppendText("You did not enter any valid URL.");
}
}
调用urlAnalyzer()
时,Result.AppendText()
转到结果文本框的事件处理方法,
private void Result_TextChanged(object sender, TextChangedEventArgs e)
{
}
每次调用 Result.AppendText()
时都会访问此事件方法,但不会将字符串附加到结果框中。当 urlAnalyzer()
函数被完全访问时,文本将出现在结果框中。
如何在执行追加语句时使追加的文本出现在文本框中?如何在每次文本追加调用时更新文本框?
WebClient.DownloadString()
在 UI 线程中执行并阻止 UI 更新。使用异步版本的下载方法:
public async void urlAnalyzer()
{
// Append text to Result box
Result.AppendText("Please wait, inspecting the URL.\n");
// Initiating WebClient to download webpage
WebClient inspecter = new WebClient();
// try-catch to avoid exception in a generic way
try
{
// stroring downloaded page in savedData
savedData = await inspecter.DownloadStringTaskAsync(webpage);
// appending downloaded html in Result box
Result.AppendText(savedData);
}
catch
{
Result.AppendText("You did not enter any valid URL.");
}
}