private void Button_Click(object sender, RoutedEventArgs e) { if (this.list.SelectedItem == null) { return; } FrameworkElement element = null; if (this.list.SelectedIndex == 0) { element = this.button; } else if (this.list.SelectedIndex == 1) { element = this.emf; } else if (this.list.SelectedIndex == 2) { element = this.radioButton; } if (element != null) { StringBuilder b = new StringBuilder(); XamlWriterSettings settings = new XamlWriterSettings(); settings.WriteDefaultValues = this.checkDefault.IsChecked ?? false; using (XamlWriter writer = XamlWriter.CreateWriter(b, this.checkAttributes.IsChecked ?? false, settings)) { writer.WriteElement(element); } result.Text = b.ToString(); } }
/// <summary> /// Initializes a new instance of the <see cref="XamlWriter"/> class. /// </summary> /// <param name="writer">The writer.</param> /// <param name="settings">The settings.</param> public XamlWriter(XmlWriter writer, XamlWriterSettings settings) { if (writer == null) { throw new ArgumentNullException("writer"); } if (settings == null) { throw new ArgumentNullException("settings"); } this.writer = writer; this.settings = settings; }
/// <summary> /// Creates a XAML writer for given output and settings. /// </summary> /// <param name="output">The output.</param> /// <param name="newLineOnAttributes">whether to write attributes on a new line.</param> /// <param name="settings">The settings.</param> /// <returns></returns> public static XamlWriter CreateWriter(StringBuilder output, bool newLineOnAttributes, XamlWriterSettings settings) { XmlWriterSettings xmlSettings = new XmlWriterSettings(); xmlSettings.Indent = true; xmlSettings.NewLineOnAttributes = newLineOnAttributes; xmlSettings.ConformanceLevel = ConformanceLevel.Fragment; XmlWriter writer = XmlWriter.Create(output, xmlSettings); return new XamlWriter(writer, settings); }
/// <summary> /// Gets the XAML of specified framework element. /// </summary> /// <param name="element">The element.</param> /// <param name="settings">The settings.</param> /// <returns></returns> public static string Save(FrameworkElement element, XamlWriterSettings settings) { if (element == null) { throw new ArgumentNullException("element"); } StringBuilder output = new StringBuilder(); try { using (XamlWriter writer = CreateWriter(output, false, settings)) { writer.WriteElement(element); } } catch (Exception e) { WriteException(output, e); } return output.ToString(); }