ViewModel 和 Model 之间使用 MVVM-Light 消息传递进行异步通信?

发布于 2024-10-12 11:13:36 字数 211 浏览 4 评论 0原文

我开始为 WP7 编写一个应用程序,其中涉及通过 HTTPWebRequest 检索数据,我试图找到在异步请求返回数据后将数据返回到 ViewModel 的最佳方法。

Model 和 ViewModel 被分成各自命名空间中的单独项目,因此我不确定 MVVM-Light 是否可以以这种方式工作。或者我应该使用轻量级 IoC 实现和委托/事件来在模型中的调用返回后回调 ViewModel?

I am starting off writing an app for WP7 that involves retrieving data via an HTTPWebRequest, I am trying to find the best way to get the data back to the ViewModel once the async request has returned with the data.

The Model and ViewModel are split into seperate projects in their own namespaces, so am not sure if the MVVM-Light can work in this way. Or should I just use a lightweight IoC implementation and delegates/events to call back to the ViewModel once the call in the Model has returned?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

逆光飞翔i 2024-10-19 11:13:36

尝试一下大小: http://amazedsaint.blogspot .com/2010/10/asynchronous-delegate-command-for-your.html。也非常适合测试,因为您的方法都不需要异步,您只需执行异步命令即可。

我对其进行了稍微编辑以扩展 DelegateCommand,以便我可以调用 RaiseCanExecuteChanged() 方法,如下所示:

public class AsyncDelegateCommand : DelegateCommand, ICommand
{

    BackgroundWorker _worker = new BackgroundWorker();
    Func<bool> _canExecute;

    /// <summary>
    /// The constructor
    /// </summary>
    /// <param name="action">The action to be executed</param>
    /// <param name="canExecute">Will be used to determine if the action can be executed</param>
    /// <param name="completed">Will be invoked when the action is completed</param>
    /// <param name="error">Will be invoked if the action throws an error</param>
    public AsyncDelegateCommand(Action action,
                                Func<bool> canExecute = null,
                                Action<object> completed = null,
                                Action<Exception> error = null
                                ) : base(action, canExecute)
    {
    ...
    }
}

希望有帮助。

Try this out for size: http://amazedsaint.blogspot.com/2010/10/asynchronous-delegate-command-for-your.html. Great for testing as well, as none of your methods need to be Asynchronous, you just execute your Async Command.

I edited it slightly to extend the DelegateCommand so I could call RaiseCanExecuteChanged() method, as below:

public class AsyncDelegateCommand : DelegateCommand, ICommand
{

    BackgroundWorker _worker = new BackgroundWorker();
    Func<bool> _canExecute;

    /// <summary>
    /// The constructor
    /// </summary>
    /// <param name="action">The action to be executed</param>
    /// <param name="canExecute">Will be used to determine if the action can be executed</param>
    /// <param name="completed">Will be invoked when the action is completed</param>
    /// <param name="error">Will be invoked if the action throws an error</param>
    public AsyncDelegateCommand(Action action,
                                Func<bool> canExecute = null,
                                Action<object> completed = null,
                                Action<Exception> error = null
                                ) : base(action, canExecute)
    {
    ...
    }
}

Hope that's of help.

心是晴朗的。 2024-10-19 11:13:36

您确定您在正确的位置提出请求吗?通常,模型仅包含您想要存储/传输的数据,视图模型将处理该模型属性的转换,以便视图可以显示(或编辑)该模型。我想模型对象是您将从 HttpWebRequest 中返回的对象,因此您可能需要考虑使用 Service 类来处理数据检索,尽管这对于您的应用程序来说可能有点过分了。

不管怎样,我强烈建议您在同一个地方处理请求和解析响应;否则,您会将逻辑分散到多个地方,这只会变得混乱且难以维护。

如果您需要一些了解 MVVM 的帮助,这里有一些资源可能会对

Are you sure you're making the request in the right place? Typically the Model contains simply the data that you want to store/transfer and the View Model would handle transforming that Model's properties so that a View can display (or edit) that Model. I would imagine that Model objects are what you're going to get back from the HttpWebRequest, and so you may want to consider using a Service class to handle retrieving the data, though that may be overkill for your application.

Either way, I'd highly reccomend that you handle the request and the parsing of the response in the same place; otherwise you are spreading the logic across multiple places, which is just going to get confusing and hard to maintain.

If you need some help understanding MVVM, here are a number of resources that you may find helpf

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