Cal、EventAggregator 和 Application.Deactivated 出现问题

发布于 2024-11-24 16:15:58 字数 1076 浏览 3 评论 0原文

我在 CAL 和事件聚合器方面遇到了一个有趣的问题。我试图在应用程序停用或激活(application.activated 和 application.deactivated)时发布事件。我的一些模块有弹出框,当应用程序失去焦点时我想隐藏它们。

这是

app.xaml.cs

protected override void OnStartup(StartupEventArgs e)
{
    eventAggregator.GetEvent<AppDeactivatedEvent>().Subscribe(RunIt);

    Deactivated += (s, a) => eventAggregator.GetEvent<AppDeactivatedEvent>().Publish(EmptyPayload.Empty);
    Deactivated += (s, a) => Console.WriteLine("Deactivated - in app.xaml"); 
    Activated += (s, a) => eventAggregator.GetEvent<AppActivatedEvent>().Publish(EmptyPayload.Empty);

    base.OnStartup(e);
}

下面的一个小代码示例Module.cs

ea.GetEvent<AppActivatedEvent>().Subscribe(presenter.AppDeactivated);

Presenter

public void AppDeactivated(EmptyPayload empty)
{
    Console.WriteLine("App Deactivated - Module");
}

发生的情况是,当应用程序失去焦点时,我会在控制台中看到以下内容 已停用 - 在 app.xaml 中 当应用程序重新获得焦点时,我会得到以下内容 应用程序已停用 - 模块

关于事件聚合器为何要等到应用程序重新获得焦点以触发事件的任何想法。

谢谢

I'm having an interesing problem with CAL and the event aggregator. I am attempting to publish an event when the app is deactivated or activated (application.activated and application.deactivated). Some of my modules have popup boxes that a I want to hide when the application loses focus.

this is a small code sample below

app.xaml.cs

protected override void OnStartup(StartupEventArgs e)
{
    eventAggregator.GetEvent<AppDeactivatedEvent>().Subscribe(RunIt);

    Deactivated += (s, a) => eventAggregator.GetEvent<AppDeactivatedEvent>().Publish(EmptyPayload.Empty);
    Deactivated += (s, a) => Console.WriteLine("Deactivated - in app.xaml"); 
    Activated += (s, a) => eventAggregator.GetEvent<AppActivatedEvent>().Publish(EmptyPayload.Empty);

    base.OnStartup(e);
}

Module.cs

ea.GetEvent<AppActivatedEvent>().Subscribe(presenter.AppDeactivated);

presenter

public void AppDeactivated(EmptyPayload empty)
{
    Console.WriteLine("App Deactivated - Module");
}

What happens is that when the app loses focus I get the following in the console
Deactivated - in app.xaml
When the App regains focus I get the below
App Deactivated - Module

Any ideas on why the event aggregator would wait until the app regains focus for the event to fire.

Thanks

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

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

发布评论

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

评论(1

缺⑴份安定 2024-12-01 16:15:58

原来我的活动订阅有问题。没问题,只是眼睛累了

turns out I had an issue with my event subscription. no problem, just tired eyes

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