从下载的文本文件更新文本块 = RPC_E_WRONG_THREAD WP8.1

Update text block from downloaded text file = RPC_E_WRONG_THREAD WP8.1

我正在学习 Windows Phone 8.1 开发,我可能在编程方面做了一些完全错误的事情

需求:我想使用HttpClient()从网上下载一个文本文件并显示在TextBlock1

从各种教程中我发现了以下内容:

    public async void DownloadDataAsync()
    {
        string data = "some link to Textfile.txt";
        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync(data);
        HttpContent content = response.Content;
        string result = await content.ReadAsStringAsync();
        UpdateTextBlock1(result);
    }

然后是其他功能。

    public void UpdateTextBlock1(string result)
    {

        TextBlock1.Text = result;
    }

    private void BtnDownloadData_Click(object sender, RoutedEventArgs e)
    {

        Task t = new Task(DownloadDataAsync);
        t.Start();

    }

代码启动得很好 - 按下按钮,我收到 RPC_E_WRONG_THREAD。

是不是我在所有线程都没有结束的时候调用方法?我如何有效地编写代码以便 TextBlock1 更新为 txt 数据?

感谢您的理解,编程中的小步骤,我找不到 google 的相关答案。 (可能我还不知道怎么问?)

您需要像这样更新 UI 线程上的文本块:

Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => 
{
  TextBlock1.Text = result;
});

关于这个主题的帖子很多。