Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

c# - 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 :

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?

Anyone has a example? Is it even possible or is there a better way?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For debugging purposes, you can use System.Diagnostics.Debug.WriteLine(), which will log on the attached debugger output screen.

See: 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...