SCSF:将消息附加到 LogView 中。使用事件发布
我在 Infrastructure.Module 项目中创建了一个新视图(LogView)。该视图将用作 VS 中类似 LogViewer 的输出窗口。我想在这个 LogView 中从不同的模块写入不同的状态消息。
我还创建了一个 LogWriter 类,它正在发布一个事件以将消息写入 LogView
我在整个应用程序中访问此 LogWriter 类时遇到问题..请告诉我如何使用它...
public class LogWriter
{
[EventPublication(EventTopicNames.WriteToLog, PublicationScope.Global)]
public event EventHandler<EventArgs<string>> WriteToLog;
private void OnWriteToLog(EventArgs<string> eventArgs)
{
if (WriteToLog != null)
{
WriteToLog(null, eventArgs);
}
}
public void WriteMsg(string msg)
{
OnWriteToLog(new EventArgs<string>(msg));
}
}
并且在 LogView 事件订阅中
[EventSubscription(EventTopicNames.WriteToLog, ThreadOption.UserInterface)]
public void OnWriteToLog(object sender, EventArgs<string> eventArgs)
{
this.txtLogs.AppendText(eventArgs.Data + Environment.NewLine);
}
请建议我的解决方案
LogWriter 类位于 Infrastructure.Interface 项目中 LogViewer 位于 Infrastructure.Module 项目
中 在 Infrastructure.Module 的 ModuleController.cs 中,我在 WorkItem.Services 集合中添加了 LogWriter
WorkItem.Services.AddNew<LogWriter>();
,在另一个项目中,我使用
var logWriter = WorkItem.Services.Get(); 获取它 if (logWriter!= null) logWriter.WriteMsg("消息");
但它返回给我 null 。
模块加载顺序也是正确的。
I created a new View (LogView) in Infrastructure.Module project. This view will be used as LogViewer like output window in VS. i want to write different status messags in this LogView from different modules.
I also created a class LogWriter which is publishing an event to write message into LogView
i am facing problem to access this LogWriter class in my whole application.. please tell me how can i use this...
public class LogWriter
{
[EventPublication(EventTopicNames.WriteToLog, PublicationScope.Global)]
public event EventHandler<EventArgs<string>> WriteToLog;
private void OnWriteToLog(EventArgs<string> eventArgs)
{
if (WriteToLog != null)
{
WriteToLog(null, eventArgs);
}
}
public void WriteMsg(string msg)
{
OnWriteToLog(new EventArgs<string>(msg));
}
}
and in LogView event subscription is
[EventSubscription(EventTopicNames.WriteToLog, ThreadOption.UserInterface)]
public void OnWriteToLog(object sender, EventArgs<string> eventArgs)
{
this.txtLogs.AppendText(eventArgs.Data + Environment.NewLine);
}
please suggest me a solution
LogWriter class is in Infrastructure.Interface project
LogViewer is in Infrastructure.Module project
In ModuleController.cs of Infrastructure.Module i Added LogWriter in WorkItem.Services Collection
WorkItem.Services.AddNew<LogWriter>();
and in one other project i am getting it using
var logWriter = WorkItem.Services.Get();
if (logWriter != null)
logWriter.WriteMsg("message");
but it is returning me null.
module loading sequence is also correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将此属性添加到 LogWriter 类中
,然后在代码中只需通过执行以下操作来访问它:
这将要求 LogWriter 所在的模块在尝试访问它的模块之前加载。如果它们是分开的,我建议添加模块依赖项。确切的方法取决于您使用的 IModuleLoader。
Add this attribute to your LogWriter class
then in your code simply access it by doing this:
This will require that the Module that the LogWriter is in is loaded before the one that tries to access it. I would recommend adding a module dependency if they are seperated. The exact way to do it depends on the IModuleLoader you are using.