我正在为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()
方法返回后,我得到一个神秘的System.InvalidCast异常。我启用了混合调试,我可以获得更多信息,但它不是很有帮助。
这是堆栈:
combase.dll!RoFailFastWithErrorContextInternal(long,unsigned long,struct _STOWED_EXCEPTION_INFORMATION_V1 * * const)未知 combase.dll!_RoFailFastWithErrorContext@4()未知 twinapi.appcore.dll!Windows :: ApplicationModel :: Core :: CoreApplication :: ForwardLocalError(struct IRestrictedErrorInfo *)未知 twinapi.appcore.dll!Windows :: ApplicationModel :: Core :: CoreApplicationFactory :: ForwardLocalError(struct IRestrictedErrorInfo *)Unknown combase.dll!CallErrorForwarder(void *,int,struct IRestrictedErrorInfo *)未知 combase.dll!_RoReportUnhandledError@4()未知 mscorlib.ni.dll!63eb2d62()未知 [下面的框架可能不正确和/或缺失,没有为mscorlib.ni.dll加载符号]
[管理到原生过渡]
mscorlib.dll!System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.RoReportUnhandledError(System.Runtime.InteropServices.WindowsRuntime.IRestrictedErrorInfo error)未知 mscorlib.dll!System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.ReportUnhandledError(System.Exception e)未知 System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()未知 System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invoker.InvokeInContext(object thisObj)Unknown mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state,bool preserveSyncCtx)Unknown mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state,bool preserveSyncCtx)Unknown System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invoker.Invoke()未知 [原产于管理过渡]
Windows.UI.dll!Windows :: UI :: Core :: CDispatcher :: ProcessInvokeItem()第794行C ++ Windows.UI.dll!Windows :: UI :: Core :: CDispatcher :: WaitAndProcessMessages(void * hEventWait)C ++ Windows.UI.dll!Windows :: UI :: Core :: CDispatcher :: ProcessEvents(Windows :: UI :: Core :: CoreProcessEventsOption options = CoreProcessEventsOption_ProcessUntilQuit)Line 390 C ++ Windows.UI.Xaml.dll!DirectUI :: FrameworkView :: Run()未知 twinapi.appcore.dll!Windows :: ApplicationModel :: Core :: CoreApplicationView :: Run(void)未知 twinapi.appcore.dll!Windows :: Foundation :: Collections :: Internal :: HashMap,struct Windows :: Foundation :: Collections :: Internal :: DefaultEqualityPredicate,struct Windows :: Foundation :: Collections :: Internal :: DefaultLifetimeTraits, struct Windows :: ApplicationModel :: Core :: Details :: SmugglableInterfaceLifetimeTraits,struct Windows :: Foundation :: Collections :: Internal :: HashMapOptions,0,1,0&gt; &gt; ::删除(unsigned int)未知 SHCore.dll!Microsoft :: WRL :: RuntimeClass,类CScalingInfoBase,struct ICurrentWindowChangeListener,类Microsoft :: WRL :: FtmBase,类Microsoft :: WRL :: Details :: Nil,class Microsoft :: WRL :: Details :: Nil ,类Microsoft :: WRL :: Details :: Nil,类Microsoft :: WRL :: Details :: Nil,类Microsoft :: WRL :: Details :: Nil,class Microsoft :: WRL :: Details :: Nil&gt;: :`vector deletion destructor'(unsigned int)未知 kernel32.dll!@ BaseThreadInitThunk @ 12()未知 ntdll.dll!__ RtlUserThreadStart()未知 ntdll.dll!__ RtlUserThreadStart @ 8()未知
这是唯一的本地人: $ exceptionstack
[9 Frames, combase.dll!_RoOriginateLanguageException@12()]
[0] combase.dll!_RoOriginateLanguageException@12() void*
[1] mscorlib.ni.dll!63eb2c8a() void*
[2] mscorlib.ni.dll!63f4ffa2() void*
[3] mscorlib.ni.dll!63f4fd61() void*
[4] System.Runtime.WindowsRuntime.ni.dll!506ef9df() void*
[5] System.Runtime.WindowsRuntime.ni.dll!506ef965() void*
[6] mscorlib.ni.dll!63823156() void*
[7] System.Runtime.WindowsRuntime.ni.dll!506ef934() void*
[8] Windows.UI.ni.dll!50e1ff16() void*
非常感谢任何帮助或指示!
答案 0 :(得分:0)
我修好了但我不确定它是最佳解决方案。如果有人想提出改进建议我就是游戏。
在我的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;
}
}