防止 MVVM ServiceAgent 事件多次挂接
开发与 WCF 服务通信的 Silverlight 应用程序。
MVVM->服务代理-> WCF 服务
因此,在我的 ViewModel 中,我有:
ServiceAgent.SearchExternalPatients(Name, (s, e) =>
{
ExternalPatients = e.Result;
});
在我的服务代理中,我有:
public void SearchExternalPatients(string name, EventHandler<SearchPatientExternalCompletedEventArgs> callback)
{
_proxy.SearchPatientExternalCompleted += callback;
_proxy.SearchPatientExternalAsync(name);
}
问题是每次我单击搜索按钮时,它都会再次连接该事件,当我得到结果时,我会多次收到它。
在 MVVM ServiceAgent 模式中取消这些事件的最佳方法是什么?
Developing a Silverlight application that is communication with a WCF Service.
MVVM -> ServiceAgent -> WCF Service
So in my ViewModel i have:
ServiceAgent.SearchExternalPatients(Name, (s, e) =>
{
ExternalPatients = e.Result;
});
in my service agent i have:
public void SearchExternalPatients(string name, EventHandler<SearchPatientExternalCompletedEventArgs> callback)
{
_proxy.SearchPatientExternalCompleted += callback;
_proxy.SearchPatientExternalAsync(name);
}
The problem is each time i click on the search button it hooks up the event again, and when i get the result back i receive it several times.
What is the best way to unhook these events in the MVVM ServiceAgent pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样做:
您可能会从编译器或 ReSharper 或 Visual Studio 的静态代码分析(取决于每个设置)收到警告,因为 lambda 正在访问在 lambda 外部修改的变量,但这应该可以正常工作。虽然我不明白为什么你首先要以这种方式分解代码。
Do it this way:
You may get a warning from the compiler or ReSharper or Visual Studio's static code analysis (depending on the settings for each) because the lambda is accessing a variable that is modified outside the lambda, but this should work correctly. Though I don't see why you would factor the code this way in the first place.
您可以利用反应式扩展来做到这一点吗?我没有对 Silverlight 做过很多工作,但 RX 往往可以减轻与事件挂钩/取消挂钩相关的许多痛苦。类似于 这个例子。
Could you perhaps utilise the Reactive Extensions to do this? I have not done a lot with Silverlight but RX tends to ease a lot of the pain associated with event hooking/unhooking. Something along the lines of this example.