mvvm light v3取消注册是否正常工作? [英] Does mvvm light v3 unregister work properly?

查看:60
本文介绍了mvvm light v3取消注册是否正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MvvM-Light v3注销是否存在问题?

Is there a problem with the MvvM-Light v3 unregister?

我正在这样注册:

 Messenger.Default.Register<DialogResponse>(this,
                                            MessageTypesUI.YesNoQueryResponse,
                                            AnswerResponse);

然后在"AnswerResponse"中

Then in "AnswerResponse"

 Messenger.Default.Unregister<DialogResponse>(MessageTypesUI.YesNoQueryResponse);

这两个方法都执行,并调用AnswerResponse,但是第二次调用两次,第三次调用三次,等等.

Both methods execute, and AnswerResponse is called, but the second time it is called twice and the third time thrice, etc.

它的作用就像忽略了我的注销".

It acts as if my "Unregister" is being ignored.

我尝试了Unregister()和普通的Unregister()

I tried it both with Unregister() and plain old Unregister()

V4发行说明中有一些内容使我怀疑版本3的注销存在问题...

There was something in the release notes for V4 that made me suspect a problem with the version 3 unregister...

推荐答案

您应将对Unregister的调用修改为传递接收者对象,而不是消息类型:

You should modify the call to Unregister to pass in the recipient object, not the message type:

Messenger.Default.Unregister<DialogResponse>(this);

如果您查看 MVVM轻代码所有取消注册的操作都使用接收者-内部使用接收者来标识将删除哪些消息处理程序.最基本的方法是Unregister(recipient),该方法注销给定收件人的所有操作.所有其他参数都允许使用更细粒度的方法.

If you look at the MVVM Light code for the messenger all unregister actions use the recipient - as internally the recipient is used for identifying which message handlers will be removed. The most basic method is Unregister(recipient), which de-registers all actions for a given recipient. All additional parameters allow for a more fine grained approach.

如果您有多个处理相同消息类型的操作,并且只想注销其中一个,则可以使用

If you have multiple actions handling the same message type and you want to unregister only one of them you can use

Messenger.Unregister<DialogMessage>(recipient, action);

仅删除其中一项操作.

修改

V.4的MVVM Light发行说明指示对Messenger的以下更改:

The MVVM Light release notes for V. 4 indicate the following changes to the Messenger:

  1. 添加的缺少方法. IMessenger界面.
  2. 添加了另一种允许使用令牌进行过滤的取消注册方法.
  3. 使Messenger.CleanupList方法成为线程安全的.

这篇关于mvvm light v3取消注册是否正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆