/// <summary> /// Reads a file to a stream and passes it to the stream-based round trip method. /// </summary> /// <param name="fileName">Xaml file path.</param> /// <param name="expressionMode">The mode of serialization for databinding and style expansion. Default: XamlWriterMode.Value.</param> /// <param name="attemptDisplay">Whether or not to display the tree if the root is a FrameworkElement or ICustomElement. Default: false</param> public void RoundTripTestFile(string fileName, XamlWriterMode expressionMode, bool attemptDisplay) { if (fileName == null) { throw new ArgumentNullException("fileName"); } if (!File.Exists(fileName)) { throw new ArgumentException("The xaml file does not exist: " + fileName + ".", "fileName"); } // Open file to stream. Stream stream = File.OpenRead(fileName); try { this.RoundTripTest(stream, expressionMode, attemptDisplay); } finally { if (stream != null) { stream.Close(); } } }
public void RoundTripTest(Stream originalStream, XamlWriterMode expressionMode, bool attemptDisplay) { if (originalStream == null) { throw new ArgumentNullException("originalStream"); } Dispatcher dispatcher = Dispatcher.CurrentDispatcher; if (dispatcher == null) { throw new InvalidOperationException("The current thread has not entered a Dispatcher."); } object firstTreeRoot = null; // // Parse original xaml. // We create a ParserContext here. It will be reused by subsequent parsing // operations. // GlobalLog.LogStatus("Constructing object tree using LoadXml..."); _parserContext = new ParserContext(); _parserContext.BaseUri = PackUriHelper.Create(new Uri("siteoforigin://")); firstTreeRoot = ParseXaml(originalStream); RoundTripTestObject(firstTreeRoot, expressionMode, attemptDisplay); }
public static string SerializeObjectTree(object objectTree, XamlWriterMode expressionMode) { StringBuilder sb = new StringBuilder(); TextWriter writer = new StringWriter(sb); XmlTextWriter xmlWriter = null; try { // Create XmlTextWriter xmlWriter = new XmlTextWriter(writer); // Set serialization mode xmlWriter.Formatting = Formatting.Indented; XamlDesignerSerializationManager manager = new XamlDesignerSerializationManager(xmlWriter); manager.XamlWriterMode = expressionMode; // Serialize SerializeObjectTree(objectTree, manager); } finally { if (xmlWriter != null) { xmlWriter.Close(); } } return(sb.ToString()); }
/// <summary> /// Converts a xaml string to a stream and passes it to the stream-based round trip method. /// </summary> /// <param name="xaml">Xaml string.</param> /// <param name="expressionMode">The mode of serialization for databinding and style expansion. Default: XamlWriterMode.Value.</param> /// <param name="attemptDisplay">Whether or not to display the tree if the root is a FrameworkElement or ICustomElement. Default: false</param> public void RoundTripTest(string xaml, XamlWriterMode expressionMode, bool attemptDisplay) { if (xaml == null) { throw new ArgumentNullException("xaml"); } _originalXaml = xaml; Stream stream = IOHelper.ConvertTextToStream(xaml); try { this.RoundTripTest(stream, expressionMode, attemptDisplay); } finally { if (stream != null) { stream.Close(); } _originalXaml = null; } }
public static string SerializeObjectTree(object objectTree, XamlWriterMode expressionMode) { StringBuilder sb = new StringBuilder(); TextWriter writer = new StringWriter(sb); XmlTextWriter xmlWriter = null; try { // Create XmlTextWriter xmlWriter = new XmlTextWriter(writer); // Set serialization mode XamlDesignerSerializationManager manager = new XamlDesignerSerializationManager(xmlWriter); manager.XamlWriterMode = expressionMode; // Serialize System.Windows.Markup.XamlWriter.Save(objectTree, manager); } finally { if (xmlWriter != null) xmlWriter.Close(); } return sb.ToString(); }
/// <summary> /// Does this: /// 1. Fires PreFirstDisplay event. /// 2. Displays the first object tree if possible and attemptDisplay is true. /// 3. Fires FirstDisplay event if the first tree has been displayed. /// 4. Fires PreFirstSerialization event. /// 5. Serializes the first object tree to a xaml string. /// 6. Parses the serialized xaml string to another object tree. /// 7. Fires PreSecondDisplay event. /// 8. Displays the second object tree if possible and attemptDisplay is true. /// 9. Fires SecondDisplay event if the second tree has been displayed. /// 10. Fires PreSecondSerialization event. /// 11. Serializes the second object tree to a xaml string. /// 12. Compares the first tree with the second tree if DoTreeComparison is true. /// 13. Compares the first serialized xaml string with the second serialized xaml string if DoXamlComparison is true. /// </summary> /// <param name="firstTreeRoot">Root of the tree.</param> /// <param name="expressionMode">The mode of serialization for databinding and style expansion. Default: XamlWriterMode.Value.</param> /// <param name="attemptDisplay">Whether or not to display the tree if the root is a FrameworkElement or ICustomElement. Default: false</param> /// <remarks>This helper functions must be called from the Dispatcher Thread.</remarks> public void RoundTripTestObject(object firstTreeRoot, XamlWriterMode expressionMode, bool attemptDisplay) { Dispatcher dispatcher = Dispatcher.CurrentDispatcher; // // Enqueue the round trip test routine. // dispatcher.BeginInvoke(DispatcherPriority.Normal, new AsyncRoundTripTestCallback(AsyncRoundTripTest), firstTreeRoot, expressionMode, attemptDisplay); // // Setup ShutDown handler to catch unexpected shutdowns. // Set appropriate flag to track our state. // dispatcher.ShutdownFinished += new EventHandler(_ValidatingExitDispatcher); // // We do this so we have only one dispatcher during // all the serialization. // Dispatcher.Run(); }
/// <summary> /// Serializes an object tree to a file. /// </summary> /// <param name="objectTree">The tree to serialize.</param> /// <param name="expressionMode">The mode of serialization for databinding and style expansion. Default: XamlWriterMode.Value.</param> /// <param name="fileName">The name of the xaml file that will be created.</param> public static void SerializeObjectTree(object objectTree, XamlWriterMode expressionMode, string fileName) { if (objectTree == null) { throw new ArgumentNullException("objectTree"); } if (fileName == null) { throw new ArgumentNullException("fileName"); } FileStream fs = null; StreamWriter sw = null; try { string outer = SerializeObjectTree(objectTree, expressionMode); fs = new FileStream(fileName, FileMode.Create); sw = new StreamWriter(fs); sw.Write(outer); } finally { if (null != sw) { sw.Close(); } if (null != fs) { fs.Close(); } } }
/// <summary> /// Callback routine to complete the round trip test. /// </summary> public void AsyncRoundTripTest(object firstTreeRoot, XamlWriterMode expressionMode, bool attemptDisplay) { try { object secondTreeRoot = null; string firstSerialized = String.Empty; string secondSerialized = String.Empty; _iteration++; if (null == firstTreeRoot) { throw new Exception("First tree root is null."); } _firstTreeRoot = firstTreeRoot; // Fire PreFirstDisplay event if (PreFirstDisplay != null) { PreFirstDisplay(_firstTreeRoot); } // // Display first tree. // if (attemptDisplay) { _isFirst = true; GlobalLog.LogStatus("Attempting to display the first tree root..."); DisplayTree(firstTreeRoot, _iteration + " " + " - first tree root", true); } // Fire PreFirstSerialization event if (PreFirstSerialization != null) { PreFirstSerialization(_firstTreeRoot); } // // Serialize first tree. // GlobalLog.LogStatus("Serializing the first tree root..."); firstSerialized = SerializeObjectTree(firstTreeRoot, expressionMode); GlobalLog.LogStatus("Done serializing the first tree root."); // Fire an event with the serialized xaml string. _FireXamlSerializedEvent(firstSerialized); if (this.CompareOriginalXaml && _originalXaml != null) { GlobalLog.LogStatus("Comparing original xaml to first serialized xaml..."); _CompareXamls(_originalXaml, firstSerialized); } // // Parse the first serialized xaml. // Nullify _parserContext (if it's not null already) so that // it can't be reused anymore // secondTreeRoot = ParseXaml(firstSerialized); _parserContext = null; _secondTreeRoot = secondTreeRoot; // Fire PreSecondDisplay event if (PreSecondDisplay != null) { PreSecondDisplay(_secondTreeRoot); } // // Display the second tree. // if (attemptDisplay) { _isFirst = false; GlobalLog.LogStatus("Attempting to display the second tree root..."); DisplayTree(secondTreeRoot, _iteration + " " + " - second tree root"); } // Fire PreSecondSerialization event if (PreSecondSerialization != null) { PreSecondSerialization(_secondTreeRoot); } // // Serialize the second tree. // GlobalLog.LogStatus("Serializing the second tree..."); secondSerialized = SerializeObjectTree(secondTreeRoot, expressionMode); GlobalLog.LogStatus("Done serializing the second tree root."); // Fire an event with the serialized xaml string. _FireXamlSerializedEvent(secondSerialized); // // Compare the first and second trees. // if (DoTreeComparison) { GlobalLog.LogStatus("Comparing object trees..."); _CompareObjectTree(firstTreeRoot, secondTreeRoot); } if (XamlWriterMode.Expression != expressionMode && AlwaysTestExpressionMode) { // For XamlWriterMode.Value, Expression mode is also verified. // // Serialize first tree with Expression Mode // GlobalLog.LogStatus("Serializing the first tree root with Expression mode..."); secondSerialized = SerializeObjectTree(firstTreeRoot, XamlWriterMode.Expression); GlobalLog.LogStatus("Done serializing the first tree root with Expression mode."); // Fire an event with the serialized xaml string. _FireXamlSerializedEvent(secondSerialized); // // Parse the serialized xaml. // secondTreeRoot = ParseXaml(secondSerialized); // Fire PreSecondDisplay event if (PreSecondDisplay != null) { PreSecondDisplay(secondTreeRoot); } // // Display the second tree. // if (attemptDisplay) { _isFirst = false; GlobalLog.LogStatus("Attempting to display the tree loaded from string serialized with Expression mode ..."); DisplayTree(secondTreeRoot, _iteration + " " + " - additional tree", false); } // // Compare the first and the additional tree parsed from string serialized with Expression mode. // if (DoTreeComparison) { GlobalLog.LogStatus("Comparing object trees..."); _CompareObjectTree(firstTreeRoot, secondTreeRoot); } } // // Compare first and second serialized xaml files. // if (DoXamlComparison) { GlobalLog.LogStatus("Comparing xamls..."); _CompareXamls(firstSerialized, secondSerialized); } } finally { // // Shutdown the dispatcher. // Set the appropriate flags to indicates the shutdown // is expected. // _roundTripTestExit = true; Dispatcher.CurrentDispatcher.ShutdownFinished -= new EventHandler(_ValidatingExitDispatcher); _ShutdownDispatcher(); } }
/// <summary> /// Specify default values and passes them to the round trip method with more parameters. /// </summary> /// <param name="treeRoot"></param> /// <param name="attemptDisplay"></param> public void RoundTripTestObject(object treeRoot, bool attemptDisplay) { XamlWriterMode expressionMode = XamlWriterMode.Value; RoundTripTestObject(treeRoot, expressionMode, attemptDisplay); }
private static bool IsValidXamlWriterMode(XamlWriterMode value) { return value == XamlWriterMode.Value || value == XamlWriterMode.Expression; }
private static bool IsValidXamlWriterMode(XamlWriterMode value) { return(value == XamlWriterMode.Value || value == XamlWriterMode.Expression); }