SCSF:将消息附加到 LogView 中。使用事件发布

发布于 2024-08-25 02:39:33 字数 1526 浏览 7 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(1

空心空情空意 2024-09-01 02:39:33

将此属性添加到 LogWriter 类中

[Service(typeof(LogWriter), AddOnDemand=true)]
public class LogWriter
{
    ...
}

,然后在代码中只需通过执行以下操作来访问它:

var logWriter = WorkItem.Service.Get<LogWriter>();
if (logWriter != null)
    logWriter.WriteMsg("message");

这将要求 LogWriter 所在的模块在尝试访问它的模块之前加载。如果它们是分开的,我建议添加模块依赖项。确切的方法取决于您使用的 IModuleLoader。

Add this attribute to your LogWriter class

[Service(typeof(LogWriter), AddOnDemand=true)]
public class LogWriter
{
    ...
}

then in your code simply access it by doing this:

var logWriter = WorkItem.Service.Get<LogWriter>();
if (logWriter != null)
    logWriter.WriteMsg("message");

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.

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