OpenReadCompletedEventArgs 的同步处理程序
我当前的项目使用异步方法通过 WebService 下载文件(由于 SL 限制)。我正在尝试编写一个使用相同接口但实际上从 XAP 同步读取文件的重写类。我已经验证我能够按预期访问 XAP 中的文件流。
但是,我不知道如何触发我的处理程序并同步读取文件。看起来 OpenReadCompletedEventArgs.Result 是我应该将流设置为的属性。但是,我无法设置此属性,因为它是只读的。
如何使用流集构造这种类型的对象?或者我应该做些什么来实现我想要的而不改变接口。
My current project uses asynchronous methods to download files through a WebService (due to SL restrictions). I'm trying to write an overriding class that uses the same interface but actually reads files synchronously from the XAP. I've verified that I'm able to access file streams in the XAP as expected.
However, I don't know how to fire my handler and read the file synchronously. It looks like OpenReadCompletedEventArgs.Result is the property that I should be setting my stream to. However, I'm unable to set this property since it is read-only.
How do I go about constructing an object of this type with the stream set? Or is there something else I should be doing to achieve what I want without changing the interfaces.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不太明白这一点,但我认为您拥有的是一些消耗
OpenReadCompletedEventArgs
的现有代码,并且您希望在检索到的流实际上是时重用此代码来自 Xap 的不是下载的资源。您将无法继承或更改
OpenReadCompletedEventArgs
类,因为它的所有构造函数都是私有的。因此,您将需要更改现有的代码。您可能只对
Stream
真正感兴趣,因此实际上您只需将大部分代码移至接受Stream
对象的函数即可。现在,使用 OpenReadCompleted 事件的代码将调用这些传递 e.Result 的函数,您的新代码可以简单地从 Xap 检索流并将其传递给这些相同的函数。Can't quite make sense of this but I think what you have is some existing code that consumes
OpenReadCompletedEventArgs
and you want to re-use this code when the stream retrieved is actually from the Xap not a downloaded resource.You will not be able to inherit or mutate the
OpenReadCompletedEventArgs
class because all its constructors are private.Hence you will need to change your existing code. You are probably only really interested in the
Stream
so in reality you need only move most of your code to functions that accept aStream
object. Now code using anOpenReadCompleted
event would then call these functions passinge.Result
and you new code can simply retrieve the stream from Xap and pass it to these same functions.我编写了一个单独的事件处理程序委托和处理程序参数来处理这种情况。我的事件处理程序参数可以从 OpenReadCompletedEventArgs 对象实例化。我确保使用相同的属性名称,以便调用代码不必更改。
此外,在异步代码的事件处理程序函数中,我只是添加了一个调用自定义事件处理程序类的匿名事件处理程序:
这样,我的处理程序仅在读取操作完成后运行。同步用户可以立即使用适当的参数调用我的处理程序。
I wrote a separate event handler delegate and handler args to handle this situation. My event handler args can be instantiated from an OpenReadCompletedEventArgs object. I made sure the same property names were used so that the calling code wouldn't have to change.
In addition, in my event handler function for the asynchronous code, I simply added an anonymous event handler that called my custom event handler class:
This way, my handler is only run after the read operation has completed. Synchronous users can just immediately call my handler with the appropriate args.