//----------------------------------------------------------------------- // // Copyright (c) Outercurve Foundation. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.OpenIdOfflineProvider { using System; using System.Diagnostics.Contracts; using System.IO; using System.Text; using System.Windows.Controls; /// /// A text writer that appends all write calls to a text box. /// internal class TextBoxTextWriter : TextWriter { /// /// Initializes a new instance of the class. /// /// The text box to append log messages to. internal TextBoxTextWriter(TextBox box) { Contract.Requires(box != null); this.Box = box; } /// /// Gets the in which the output is written. /// /// /// The Encoding in which the output is written. /// public override Encoding Encoding { get { return Encoding.Unicode; } } /// /// Gets the box to append to. /// internal TextBox Box { get; private set; } /// /// Writes a character to the text stream. /// /// The character to write to the text stream. /// /// The is closed. /// /// /// An I/O error occurs. /// public override void Write(char value) { this.Box.Dispatcher.BeginInvoke((Action)this.AppendText, value.ToString()); } /// /// Writes a string to the text stream. /// /// The string to write. /// /// The is closed. /// /// /// An I/O error occurs. /// public override void Write(string value) { this.Box.Dispatcher.BeginInvoke((Action)this.AppendText, value); } /// /// Verifies conditions that should be true for any valid state of this object. /// [ContractInvariantMethod] private void ObjectInvariant() { Contract.Invariant(this.Box != null); } /// /// Appends text to the text box. /// /// The string to append. private void AppendText(string value) { this.Box.AppendText(value); this.Box.ScrollToEnd(); } } }