尝试获取JSON文件作为StorageFile时发生System.Runtime.InteropServices.COMException

时间:2018-07-13 09:16:33

标签: c# uwp

我有一个非常简单的UWP应用。我只想将JSON文件作为可存储文件。 这是代码:

public sealed partial class MainPage : Page
{

    public MainPage()
    {
        this.InitializeComponent();
    }

    private async void GetCityButton_Click(object sender, RoutedEventArgs e)
    {
        Uri dataUri = new Uri("ms-appx:///Model/sample.json");

        StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
    }
}

这是json文件数据:

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"Drizzle","description":"light intensity drizzle","icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":81,"temp_min":279.15,"temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},"clouds":{"all":90},"dt":1485789600,"sys":{"type":1,"id":5091,"message":0.0103,"country":"GB","sunrise":1485762037,"sunset":1485794875},"id":2643743,"name":"London","cod":200}

每当我尝试调用此click方法时,我都会得到System.Runtime.InteropServices.COMException

但是,每当我尝试将其重命名为sample.txt时,都不会引发任何错误。 这是屏幕截图。

enter image description here

enter image description here

请帮助我!为什么会这样?

2 个答案:

答案 0 :(得分:2)

您需要确保将Build action设置为Content,以便该文件包含在应用程序包中。完成此操作后,代码即可按预期的方式工作:-)。

答案 1 :(得分:0)

非常简单!尝试sample.json文件属性的“ build action:content”。然后再尝试调试。我为我工作得很好。