I don't know. As I said, a concise-but-complete code example would go a
long way to improving your question.
I am not sure if the example that you are asking is what I am going to
post.
The objectives are:
1. Display the result of Console.WriteLine in Console and in
TextBlock ...
... From Window1 and from Test.Run() when the button is clicked.
2. Write the result of a command in Console and TextBlock ...
In my code I was checking the messages of minimizing the JS and CSS
code.
I didn't implement this part. Maybe saving or opening a file?
I think the TextWriterProxy would be a better option than my initial
approach as it even allows to write to other targets.
This said, this is my code (I hope this is what you are asking):
-- BEGIN Window1.xaml ---
<Window x:Class="ConsoleTest.Window1"
xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
</Window>
-- BEGIN Window1.xaml.cs ---
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
// ConsoleTest
namespace ConsoleTest {
public partial class Window1: Window {
#region Constructors
public Window1() {
// Initialize component
InitializeComponent();
// Define proxy
TextWriterProxy proxy = new TextWriterProxy();
proxy.Add(Console.Out);
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
proxy.Add(sw);
Console.SetOut(proxy);
// Dock panel
DockPanel content = new DockPanel();
// Menu
Menu menu = new Menu();
menu.Items.Add(new MenuItem { Command = TestCommands.Test,
Header = "Test" });
DockPanel.SetDock(menu, Dock.Top);
content.Children.Add(menu);
// Text block
TextBlock output = new TextBlock();
DockPanel.SetDock(output, Dock.Bottom);
content.Children.Add(output);
Content = content;
// 1: Use Text Writer Proxy - NOT WORKING FROM Test.Run()
proxy.WriteLine("Write from Window1");
output.Text = sb.ToString();
// 2: Use Console Log - NOT WORKING
//using (ConsoleLog cl = new ConsoleLog(output, Console.Out)) {
// Console.SetOut(cl);
//}
//Console.WriteLine("Write from Window1");
} // Start
#endregion // Constructors
} // Start
} // ConsoleTest
// ConsoleLog
public class ConsoleLog : TextWriter {
public override Encoding Encoding { get { return
Encoding.Default; } } // Encoding
private TextWriter _writer;
private TextBlock _output;
public ConsoleLog(TextBlock output, TextWriter writer) {
_writer = writer;
_output = output;
} // ConsoleLog
public override void Write(String s) {
_writer.Write(s);
_output.Text = String.Concat(_output.Text, s);
} // Write
} // ConsoleLog
// TestCommands
public static class TestCommands {
public static ICommand Test { get { return new TestCommand(); } }
public class TestCommand : ICommand {
// CanExecute
public Boolean CanExecute(Object parameter) {
return true;
} // CanExecute
// CanExecuteChanged
public event EventHandler CanExecuteChanged {
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
} // CanExecuteChanged
// Execute
public void Execute(Object parameter) {
TestService.Run();
} // Execute
} // TestCommand
} // TestCommands
// TestService
public static class TestService {
#region Methods
public static void Run() {
Console.WriteLine("Write from Test Service");
} // Run
#endregion // Methods
} // TestService
public class TextWriterProxy : TextWriter {
private List<TextWriter> _writers = new List<TextWriter>();
public override Encoding Encoding { get { return
Encoding.Default; } } // Encoding
public override string NewLine {
get { return base.NewLine; }
set {
foreach (TextWriter tw in _writers)
tw.NewLine = value;
base.NewLine = value;
}
} // NewLine
public void Add(TextWriter writer) {
if (!_writers.Contains(writer))
_writers.Add(writer);
} // Add
public bool Remove(TextWriter writer) {
return _writers.Remove(writer);
} // Remove
public override void Write(char value) {
foreach (TextWriter tw in _writers)
tw.Write(value);
base.Write(value);
} // Write
public override void Close() {
foreach (TextWriter tw in _writers)
tw.Close();
base.Close();
} // Close
protected override void Dispose(bool disposing) {
foreach (TextWriter tw in _writers)
tw.Dispose();
base.Dispose(disposing);
} // Dispose
public override void Flush() {
foreach (TextWriter tw in _writers)
tw.Flush();
base.Flush();
} // Flush
} // TextWriterProxy
Thank You,
Miguel