【问题标题】:Subscribing to an event in a different module in Prism在 Prism 的不同模块中订阅事件
【发布时间】:2016-06-21 05:13:09
【问题描述】:

在我的 LoginModule 视图模型中,我正在调度一个事件:

void LoginUpdate(object sender, EventArgs e)
{
    _eventAggregator.GetEvent<LoginStatusEvent>().Publish(_status);
}

EventModule

public class LoginStatusEvent : PubSubEvent<LoginStatus>
{
}

然后我尝试在不同的模块中订阅它:

public class EventModule : IModule
{
    IRegionManager _regionManager;
    IEventAggregator _eventAggregator;
    private SubscriptionToken subscriptionToken;
    private bool isLoggedIn { get; set; }

    public EventModule(IEventAggregator eventAggregator, IRegionManager regionManager)
    {
        _regionManager = regionManager;
        _eventAggregator = eventAggregator;

        LoginEventsListener();
    }

    public void Initialize()
    {

    }

    public void LoginEventsListener()
    {
        LoginStatusEvent loginStatusEvent = _eventAggregator.GetEvent<LoginStatusEvent>();

        if (subscriptionToken != null)
        {
            loginStatusEvent.Unsubscribe(subscriptionToken);
        }

        subscriptionToken = loginStatusEvent.Subscribe(LoginStatusEventHandler, ThreadOption.UIThread, false);
    }

    public void LoginStatusEventHandler(LoginStatus loginStatus)
    {
        Trace.WriteLine(">> Got it!!");

    }

}

但是,LoginStatusEventHandler 从未被解雇,我也没有收到任何错误。

【问题讨论】:

  • 你在哪里定义事件?发布者和订阅者都需要引用完全相同的类型。
  • 该事件在EventModule 中定义,在LoginModule 中触发,我也在尝试在EventModule 中订阅它
  • 它是否与“订阅”方法中的“真”标志一起使用?
  • 是的,是的。请把它放在@galakt 的答案中,我会接受它。谢谢。

标签: c# mvvm prism


【解决方案1】:

OP 在订阅事件时没有保留订阅者引用,因此在某一时刻类根本没有引用并被 GC 收集。

所以在这种情况下,它将与True 标志一起使用到Subscribe 方法中。

正如@Haukinger 所指出的:

在 Prism 文档中 https://github.com/PrismLibrary/Prism/blob/ef1a2266905a4aa3e7087955e9f7b5a7d71972fb/Documentation/WPF/30-ModularApplicationDevelopment.md#initializing-modules

Module instance lifetime is short-lived by default. After the Initialize method is called during the loading process, the reference to the module instance is released. If you do not establish a strong reference chain to the module instance, it will be garbage collected. This behavior may be problematic to debug if you subscribe to events that hold a weak reference to your module, because your module just "disappears" when the garbage collector runs.

【讨论】:

  • 当 OP 说“把它作为一个答案,我会接受”时,他的意思是,将该评论表述为一个正确的答案。这仍然是一个评论。你应该为什么这样做......
  • @CallumLinington 你是真的
  • 您可能想从 prism 文档 Note: Module instance lifetime is short-lived by default. After the Initialize method is called during the loading process, the reference to the module instance is released. If you do not establish a strong reference chain to the module instance, it will be garbage collected. This behavior may be problematic to debug if you subscribe to events that hold a weak reference to your module, because your module just "disappears" when the garbage collector runs. 中提及这一点,因为有人可能认为订阅令牌使订阅保持活动状态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
相关资源
最近更新 更多