OpenReadCompletedEventArgs 的同步处理程序

发布于 2024-11-28 12:21:29 字数 261 浏览 2 评论 0原文

我当前的项目使用异步方法通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2024-12-05 12:21:29

不太明白这一点,但我认为您拥有的是一些消耗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 a Stream object. Now code using an OpenReadCompleted event would then call these functions passing e.Result and you new code can simply retrieve the stream from Xap and pass it to these same functions.

很糊涂小朋友 2024-12-05 12:21:29

我编写了一个单独的事件处理程序委托和处理程序参数来处理这种情况。我的事件处理程序参数可以从 OpenReadCompletedEventArgs 对象实例化。我确保使用相同的属性名称,以便调用代码不必更改。

此外,在异步代码的事件处理程序函数中,我只是添加了一个调用自定义事件处理程序类的匿名事件处理程序:

public void FunctionUsingHandler(MyCustomHandler handler)
{
    WebClient c;
    ...
    c.OpenReadCompleted += new OpenReadCompletedEventHandler((sender, e) => handler(sender, new MyCustomHandlerArgs(e)));
    ...
}

这样,我的处理程序仅在读取操作完成后运行。同步用户可以立即使用适当的参数调用我的处理程序。

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:

public void FunctionUsingHandler(MyCustomHandler handler)
{
    WebClient c;
    ...
    c.OpenReadCompleted += new OpenReadCompletedEventHandler((sender, e) => handler(sender, new MyCustomHandlerArgs(e)));
    ...
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文