Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.InvalidCast异常Windows运行时_C#_Windows Runtime_Winrt Xaml_Winrt Async - Fatal编程技术网

C# System.InvalidCast异常Windows运行时

C# System.InvalidCast异常Windows运行时,c#,windows-runtime,winrt-xaml,winrt-async,C#,Windows Runtime,Winrt Xaml,Winrt Async,我正在为windows应用商店构建一个电子书管理器,并且在我的一个类上实现了IUriToStreamResolver接口。我用它来开一个epub。代码如下: public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri) { if(uri == null) { throw new Exception(); } string path = uri.AbsoluteP

我正在为windows应用商店构建一个电子书管理器,并且在我的一个类上实现了IUriToStreamResolver接口。我用它来开一个epub。代码如下:

public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
{
     if(uri == null)
     {
         throw new Exception();
     }

     string path = uri.AbsolutePath;

     return GetContent(path).AsAsyncOperation();
}


private async Task<IInputStream> GetContent(string path)
{
     path = path.TrimStart('/');
     var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(App.token);
     var stream = await file.OpenAsync(FileAccessMode.Read);
     var archive = new ZipArchive(stream.AsStream());
     var entry = archive.Entries.Where(a => a.FullName == path);
     var entryStream = entry.First().Open();
     return entryStream.AsInputStream();
}

非常感谢任何帮助或指导

我修复了它,但我不确定它是否是最佳解决方案。如果有人想建议改进,我很高兴

在我的GetContent方法中,我做了以下更改:

private async Task<IInputStream> GetContent(string path)
    {
        try
        {
            path = path.TrimStart('/');
            var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsyn(App.token);

            var archive = new ZipArchive(await file.OpenStreamForReadAsync(),ZipArchiveMode.Read);
            var entry = archive.GetEntry(path);
            var contents = new byte[entry.Length];
            var entryStream = entry.Open();
            var tempFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("temp.xhtml",CreationCollisionOption.ReplaceExisting);
            var decompressedStream = await tempFile.OpenStreamForWriteAsync();
            await entryStream.CopyToAsync(decompressedStream, (int)entry.Length);
            await decompressedStream.FlushAsync();
            decompressedStream.Dispose();
            var returnFile = await ApplicationData.Current.LocalFolder.GetFileAsync("temp.xhtml");
            var returnStream = await returnFile.OpenSequentialReadAsync();
            return returnStream;
        }
        catch (Exception e)
        {

            throw;
        }
    }

我敢打赌这是您对stream.AsStream的使用,因为它是WindowsRuntime互操作中的扩展之一。我认为StorageFile应该有自己的扩展名,例如file.OpenStreamForReadAsync或stream.AsStreamForRead。我尝试了将要编译的扩展方法的每一种组合,得到了相同的结果。您是否有机会尝试访问您的OneDrive SkyDrive新名称中的文件?没有。但默认情况下,它是一个超出允许访问区域的文件,这就是我为什么要这样做的原因我正在使用FutureAccessList。
private async Task<IInputStream> GetContent(string path)
    {
        try
        {
            path = path.TrimStart('/');
            var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsyn(App.token);

            var archive = new ZipArchive(await file.OpenStreamForReadAsync(),ZipArchiveMode.Read);
            var entry = archive.GetEntry(path);
            var contents = new byte[entry.Length];
            var entryStream = entry.Open();
            var tempFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("temp.xhtml",CreationCollisionOption.ReplaceExisting);
            var decompressedStream = await tempFile.OpenStreamForWriteAsync();
            await entryStream.CopyToAsync(decompressedStream, (int)entry.Length);
            await decompressedStream.FlushAsync();
            decompressedStream.Dispose();
            var returnFile = await ApplicationData.Current.LocalFolder.GetFileAsync("temp.xhtml");
            var returnStream = await returnFile.OpenSequentialReadAsync();
            return returnStream;
        }
        catch (Exception e)
        {

            throw;
        }
    }