WinRT的:加载与GetFileFromApplicationUriAsync静态数据() [英] WinRT: Loading static data with GetFileFromApplicationUriAsync()

查看:362
本文介绍了WinRT的:加载与GetFileFromApplicationUriAsync静态数据()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据在我的Windows 8应用程序,应运而只是一些静态的数据。事实上:这是应该得到反序列化一个简单的XML文件

I have some data in my windows 8 application which should be shipped with and is just some static data. In fact: It's a simple xml file which should get deserialized.

中的数据被保存在资产\\ data.xml中(资产从空白应用模板的默认文件夹)。

The data is saved in Assets\data.xml (Assets is the default folder from the Blank App Template).

我用这块code来访问它:

I'm using this piece of code to access it:

private static async Task<MyObject> Load()
{
    if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
    {
        return new SampleData();
    }

    var uri = new Uri("ms-appx:///Assets/data.xml");
    Debug.WriteLine("Getting file from Application");
    var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
    Debug.WriteLine("Opening file for reading async");
    var stream = await file.OpenStreamForReadAsync();

    var serializer = new XmlSerializer(typeof(MyObject));

    Debug.WriteLine("Begin deserialization");
    var result = (MyObject)serializer.Deserialize(stream.AsInputStream().AsStreamForRead());

    return result;
}

调用方式:

public static MyObject GetMyObject()
{
    if (_myObject == null)
    {
        _myObject = Load().Result;
    }

    return _myObject;
}

有关的搞笑的部分是:

The "funny" part about that is:

如果我设置为 VAR URI =新的URI(...)断点; 和使用F11逐句通过code,一切正常。我得到的所有调试行和我的应用显示了静态数据通缉。

If I set a breakpoint at var uri = new Uri(...); and use F11 to step through the code, everything works as expected. I get all of the debug lines and my Application shows the static data as wanted.

如果我没有设置断点,并在这片code的不踩,我只得到的调试后,来自应用程序的文件仅此而已发生。看来, GetFileFromApplicationUriAsync()一去不复返。我等了五分钟以上,但仍然没有happend。

If I don't set a breakpoint and don't step over this piece of code, I only get the Debug output of Getting a file from Application and nothing more happens. It seems that GetFileFromApplicationUriAsync() never comes back. I waited more than five minutes but still nothing happend.

任何人有什么想法?

推荐答案

感谢张贴code。请试着改变你的方法加载如下:

Thanks for posting code. Please try to change your method Load as the following:

//your code
var file = await StorageFile.GetFileFromApplicationUriAsync(uri).AsTask().ConfigureAwait(false);
//your code
var stream = await file.OpenStreamForReadAsync().ConfigureAwait(false);
//your code

这里的主要区别是的 AsTask()。ConfigureAwait(假)

编辑:

很好听,它的工作。解释很简单:当您使用 task.Result task.Wait()上结合GUI线程与的await 关键字你造成的僵局。这是因为等待 code是同样的情况下恢复后,这是调用(在你的情况 - GUI线程)。而且,由于GUI线程目前正在等待任务完成(通过结果等待())的僵局出现和$ C $在ç等待关键字永远不会被调用。 ConfigureAwait(假)指定当前上下文可以忽略不计,从而让您的code成功完成。 的http://在这个位置更多细节blog.stephencleary.com/2012/07/dont-block-on-async-$c$c.html

Good to hear that it's working. Explanation is quite simple: when you use task.Result or task.Wait() on GUI thread in conjunction with await keyword you're causing deadlock. This happens because after awaiting code is resumed on the same context it was invoked (in your case - GUI thread). And because GUI thread currently waiting for task to complete (via Result or Wait()) deadlock arises and code after await keyword will never be invoked. ConfigureAwait(false) specifies that current context can be ignored and thus allows your code to complete successfully. More details on this here: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

这篇关于WinRT的:加载与GetFileFromApplicationUriAsync静态数据()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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