将 Messenger 与枚举令牌一起使用似乎无法按预期工作
请帮助..我正在尝试使用信使类进行跨视图通信,特别是我想将所有错误(异常)路由到主视图,在主视图中我可以弹出一个漂亮的用户界面,其中包含错误和一些建议。
我在共享项目中有一个静态类,其中所有应用程序消息类型作为枚举。 (在下面的示例中减少到一个)
public static class AppMessages
{
enum MessageTypes
{
RaiseError
}
public static class RaiseErrorMessage
{
public static void Send(Exception ex)
{
Messenger.Default.Send(ex, MessageTypes.RaiseError);
}
public static void Register(object recipient, Action<Exception> action)
{
Messenger.Default.Register(recipient, MessageTypes.RaiseError, action);
}
}
}
例如,要注册,我会调用同一共享项目中静态类上的静态方法。
AppMesssages.RaiseErrorMessage.Register(this,OnRaiseErrorMessage);
要发送,我使用...
AppMessages.RaiseErrorMessage.Send(e);
现在我遇到的问题是,除非我更改这些标记对相同的值说“1”我没有收到消息,但我一生都看不出为什么使用枚举不起作用?
所以要明确的是,我只有在使用时才会收到消息...
public static class RaiseErrorMessage
{
public static void Send(Exception ex)
{
Messenger.Default.Send(ex, 1);
}
public static void Register(object recipient, Action<Exception> action)
{
Messenger.Default.Register(recipient, 1, action);
}
}
我只是一直盯着代码,现在我的大脑冻结了,告诉我我做错了什么。非常感谢您的回复。
Please help.. I'm trying to use the messenger class for cross view comms, specifically I want to route all errors (exceptions) through to the main view where I can pop up a nice UI with the error and some suggestions what to do.
I have a static class in a shared project with all my apps message types as an enumeration. (reduced down to one in the example below)
public static class AppMessages
{
enum MessageTypes
{
RaiseError
}
public static class RaiseErrorMessage
{
public static void Send(Exception ex)
{
Messenger.Default.Send(ex, MessageTypes.RaiseError);
}
public static void Register(object recipient, Action<Exception> action)
{
Messenger.Default.Register(recipient, MessageTypes.RaiseError, action);
}
}
}
To register I call into a static method on a static class within the same shared project, for example..
AppMesssages.RaiseErrorMessage.Register(this,OnRaiseErrorMessage);
To send I use ...
AppMessages.RaiseErrorMessage.Send(e);
Now the issue I have is unless I change those tokens to the same value say "1" I don't recieve the message, but I can't see for the life of me why the use of the enum doesn't work?
So to be clear I only get the message if I use...
public static class RaiseErrorMessage
{
public static void Send(Exception ex)
{
Messenger.Default.Send(ex, 1);
}
public static void Register(object recipient, Action<Exception> action)
{
Messenger.Default.Register(recipient, 1, action);
}
}
I just keep staring at the code and now I have brain freeze, tell me what I doing wrong. Many Thanks for any replies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@user1014297,我已经使用枚举实现了类似的解决方案,并且它有效。这可能是线程问题和/或用于注册接收器的实例超出范围。
我的解决方案使用 MEF 以及 MVVM Light,我将各种 MEF 模块的异常发送到 MEF 启动应用程序。所有异常均使用单个 UI 显示。为了便于说明,我简化了下面的代码
注册:
发送消息:
@user1014297, I have implemented a similar solution using an Enum and it works. It might be a threading issue and/or that the instance that is used for registering the receiver run out of scope.
My solution uses MEF as well as MVVM Light, I send exceptions from various MEF modules to the MEF start-up application. All exception are shown using single UI. I have simplified the code below for illustration purposes
Registration:
Sending Message: