如何在 Windows8 应用程序中使用 ReadTextAsync(StorageFile file) 同步获取返回值 [英] How to get return value synchronously using ReadTextAsync(StorageFile file) in Windows8 app

查看:14
本文介绍了如何在 Windows8 应用程序中使用 ReadTextAsync(StorageFile file) 同步获取返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我的代码是一个非常简单的测试,用于在 Windows 8 风格的应用程序中写入和读取文件.这里首先将字符串Jessie"写入dataFile.txt,然后由程序读取,以便更新xaml中Textblock的Text属性.

Basiclly, my code is a very simple test for write and read files in a windows 8 style app. Here, a string "Jessie" is firstly written to dataFile.txt, and then it is read by the program so that the Text property of a Textblock in xaml could be updated.

从 msdn 示例中,我知道 WriteTextAsync 和 ReadTextAsync 用于在 Windows 8 编程中对文件进行 dataFile 访问.但是,测试结果是 WriteTextAsync 函数实际有效,而 ReadTextAsync 函数无效(我可以看到Jessie"已写入 dataFile.txt 的真实 txt 文件,但变量 str 始终为空).

From msdn samples, I knew that WriteTextAsync and ReadTextAsync are for dataFile access to file in Windows 8 programming. However, the test result is that WriteTextAsync function actually works whereas ReadTextAsync does not(I can see the real txt file that "Jessie" has been written to dataFile.txt, however the variable str is always null).

我在互联网上看到过类似的问题,但这些问题的答案对我来说都没有意义.许多答案提到问题可能是 ReadTextAsync 是一个异步函数,例如使用 Task 但他们的所有解决方案都不适用于我的代码.我的问题是如何使用 ReadTextAsync 同步获取返回值,或者是否有任何其他方法可以从 Windows8 应用程序中的 txt 文件中读取数据,以便我可以同步更新 UI?

I have seen similar questions from internet, but none of the answers for them make sense to me. Many answer mentioned the problem might be ReadTextAsync is a async function, such like using Task but all of their solutions doen't work to my code. My question is how can I get the return value synchronously using ReadTextAsync or is there any other method to read data from txt file in Windows8 app so that I can update the UI synchronously?

代码如下:

public sealed partial class MainPage : Page
{  
    Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

    public MainPage()
    {
        this.InitializeComponent();

        Write();
        Read();
    }

    async void Write()
    {
        StorageFile sampleFile = await localFolder.CreateFileAsync("dataFile.txt", Windows.Storage.
                  CreationCollisionOption.ReplaceExisting);

        await FileIO.WriteTextAsync(sampleFile, "Jessie");
    }

    async void Read()
    {
        try
        {      
            StorageFile sampleFile = await localFolder.GetFileAsync("dataFile.txt");
            string str = await FileIO.ReadTextAsync(sampleFile);

            Text.DataContext = new Test(str, 1);
        }
        catch (FileNotFoundException)
        {

        }
    }

    public class Test
    {
        public Test() { }

        public Test(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public string Name { get; set; }
        public int Age { get; set; }

        // Overriding the ToString method
        public override string ToString()
        {
            return "Name: " + Name + "\r\nAge: " + Age;
        }
    }
}

非常感谢.

推荐答案

你应该让 async 方法在任何时候返回 Task 而不是 void可能的.您可能会发现我的async/await 介绍帖子很有帮助.我还有一篇关于异步构造函数的博文.

You should have your async methods return Task rather than void whenever possible. You may find my intro to async/await post helpful. I also have a blog post on asynchronous constructors.

构造函数不能是async,但是你可以从一个构造函数开始一个异步进程:

Constructors cannot be async, but you can start an asynchronous process from a constructor:

public MainPage()
{
  this.InitializeComponent();
  Initialization = InitializeAsync();
}

private async Task InitializeAsync()
{
  await Write();
  await Read();
}

public Task Initialization { get; private set; }

async Task Write() { ... }
async Task Read() { ... }

这篇关于如何在 Windows8 应用程序中使用 ReadTextAsync(StorageFile file) 同步获取返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆