From: shapper on
Hello,

I have a Console application that performs some actions and in the
window I can see text that is saying what is doing.
I know created a WPF application with the same code.

Is it possible to show the same output text in a TextBlock?

Thanks,
Miguel
From: Peter Duniho on
shapper wrote:
> Hello,
>
> I have a Console application that performs some actions and in the
> window I can see text that is saying what is doing.
> I know created a WPF application with the same code.
>
> Is it possible to show the same output text in a TextBlock?

Yes. You could redirect the standard output of the Console class by
calling SetOut(). But IMHO a better way would be to abstract your
output via a custom class (e.g. a subclass of TextWriter you've
created), and then use that abstraction to allow for the output to
continue to go to the Console's standard output while intercepting it
for your WPF control.

Pete
From: shapper on
On Nov 19, 6:21 pm, Peter Duniho <no.peted.s...(a)no.nwlink.spam.com>
wrote:
> Yes.  You could redirect the standard output of the Console class by
> calling SetOut().  But IMHO a better way would be to abstract your
> output via a custom class (e.g. a subclass of TextWriter you've
> created), and then use that abstraction to allow for the output to
> continue to go to the Console's standard output while intercepting it
> for your WPF control.

I am not sure if I fully understand what you are saying but I am
starting as follows:

private class ConsoleLog : TextWriter {

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

_writer should be the original writer and output should be the
TextBlock control where I would like to display the output.

Then I would use something like:

using (ConsoleLog cl = new ConsoleLog(tbOutput, Console.Out)) {
Console.SetOut(cl);
}

Is this what you mean?

Thanks,
Miguel
From: Peter Duniho on
shapper wrote:
> [...]
> Then I would use something like:
>
> using (ConsoleLog cl = new ConsoleLog(tbOutput, Console.Out)) {
> Console.SetOut(cl);
> }
>
> Is this what you mean?

Not quite, but that approach looks like it should work too.

What I really meant was for you to have a class like your ConsoleLog
class, which you use _instead_ of the Console class directly for your
logging. That class would then handle directing the output as desired
(either to the TextBlock, to the Console, or both).

There are lots of different ways you can do this. Just pick the one
that seems to fit your existing architecture the best.

Pete
From: shapper on
Hi,

I am using the TextWriterProxy in the WPF application as follows:

TextBlock output = new TextBlock();

TextWriterProxy proxy = new TextWriterProxy();
proxy.Add(Console.Out);

StringBuilder sb = new StringBuilder();
StringWriter resultStringWriter = new StringWriter(sb);
proxy.Add(resultStringWriter);
output.Text = sb.ToString();

However, text block does not show any text. Any idea how to do this
integration?

The TextWriterProxy code is as follows:

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

Thanks,
Miguel