简体   繁体   English

在 WPF 应用程序中使用带有 MVVM 的屏幕登录排序

[英]Using sort of logging on screen with MVVM in a WPF application

In a Console application you can use "Console.WriteLine" to log an event, in WPF you can do the same using a RichTextBox code behind something like this:在控制台应用程序中,您可以使用“Console.WriteLine”记录事件,在 WPF 中,您可以使用 RichTextBox 代码执行相同操作,如下所示:

public Logger(RichTextBox loggingRichTextBox)
{
    _content = new FlowDocument();

    LoggingRichTextBox = loggingRichTextBox;
    LoggingRichTextBox.Document = _content;
}

I can't figure out how to do this with MVVM as you can't pass the RichTextBox control to the ViewModel?我无法弄清楚如何使用 MVVM 执行此操作,因为您无法将 RichTextBox 控件传递给 ViewModel?

Anyone has a example?有人有例子吗? Is it even possible or is there a better way?甚至有可能还是有更好的方法?

For debugging purposes, you can use System.Diagnostics.Debug.WriteLine() , which will log on the attached debugger output screen.出于调试目的,您可以使用System.Diagnostics.Debug.WriteLine() ,它将登录附加的调试器 output 屏幕。

See: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.writeline?view=net-5.0请参阅: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.writeline?view=net-5.0

But if you want to display the output for the user then it is possible to bind a TextBlock Text property to the ViewModel and append logged lines to it.但是,如果您想为用户显示 output ,则可以将TextBlock Text 属性绑定到 ViewModel 并将 append 记录的行绑定到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM