//Save stackpanel public void saveStackPanel(string fileName, StackPanel stackPanelName) { if (File.Exists(@fileName + ".xaml") == true) { File.Delete(@fileName + ".xaml"); } StringBuilder outstr = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; settings.NewLineOnAttributes = true; XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings)); dsm.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(stackPanelName, dsm); string savedControls = outstr.ToString(); File.WriteAllText(@fileName + ".xaml", savedControls); }
private void menuItemImport_Click(object sender, RoutedEventArgs e) { OpenFileDialog openfiledlg = new OpenFileDialog(); openfiledlg.InitialDirectory = "c:\\"; openfiledlg.Filter = "XAML(*.xaml)|*.xaml"; openfiledlg.RestoreDirectory = true; if (openfiledlg.ShowDialog() == true) { System.IO.FileStream fStream; if (System.IO.File.Exists(openfiledlg.FileName)) { fStream = new System.IO.FileStream(openfiledlg.FileName, System.IO.FileMode.OpenOrCreate); richTextBoxXaml.Document.Blocks.Clear(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; XamlDesignerSerializationManager designSerialization = new XamlDesignerSerializationManager(XmlWriter.Create(fStream, settings)); designSerialization.XamlWriterMode = XamlWriterMode.Expression; StreamReader sr = new StreamReader(fStream); String str = sr.ReadToEnd(); richTextBoxXaml.Document.Blocks.Add(XAMLParser.GetFomattedXAML(str)); richTextBoxXaml.ScrollToHome(); fStream.Close(); ImportXaml(); } } }
public static void Initialise(string key) { if (null == resourceDictionary) { Uri uri = new Uri("LitDev;component/Themes/Generic.xaml", UriKind.Relative); resourceDictionary = (ResourceDictionary)Application.LoadComponent(uri); } string value = ""; if (!styles.TryGetValue(key, out value)) { Style templateStyle = (Style)resourceDictionary[key]; StringBuilder sb = new StringBuilder(); XmlWriter writer = XmlWriter.Create(sb); XamlDesignerSerializationManager mgr = new XamlDesignerSerializationManager(writer); mgr.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(templateStyle, mgr); value = sb.ToString(); styles[key] = value; } style = (Style)XamlReader.Parse(value); }
internal static string ExportAsXAML_List(OutlinerDocument Document) { FlowDocument wholeDocument = new FlowDocument(); wholeDocument.FontFamily = UVOutliner.Settings.DefaultFontFamily; wholeDocument.FontSize = UVOutliner.Settings.DefaultFontSize; List newList = new List(); wholeDocument.Blocks.Add(newList); DumpRtfRecursively_List(newList, Document.RootNode); MemoryStream outStream = new MemoryStream(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; XmlWriter writer = XmlWriter.Create(outStream, settings); XamlDesignerSerializationManager manager = new XamlDesignerSerializationManager(writer); XamlWriter.Save(wholeDocument, manager); outStream.Seek(0, SeekOrigin.Begin); StreamReader reader = new StreamReader(outStream); return(reader.ReadToEnd()); }
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()); }
private T Clone <T>(T obj) { var sb = new StringBuilder(); using (var writer = XmlWriter.Create(sb, new XmlWriterSettings { Indent = false, CheckCharacters = false, CloseOutput = true, DoNotEscapeUriAttributes = false, ConformanceLevel = ConformanceLevel.Fragment, OmitXmlDeclaration = true, NamespaceHandling = NamespaceHandling.Default, })) { var mgr = new XamlDesignerSerializationManager(writer); mgr.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(obj, mgr); using (var stringReader = new StringReader(sb.ToString())) using (var xmlReader = XmlReader.Create(stringReader)) { return((T)XamlReader.Load(xmlReader)); } } }
public static string GetXamlAsString(object?obj) { if (obj is null) { return(string.Empty); } var writerSettings = new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true, NamespaceHandling = NamespaceHandling.OmitDuplicates, Encoding = Encoding.UTF8 }; var xamlString = new StringBuilder(); var xamlDesignerSerializationManager = new XamlDesignerSerializationManager(XmlWriter.Create(xamlString, writerSettings)) { XamlWriterMode = XamlWriterMode.Expression }; using (new XamlSerializationHelper().ApplyHelpers()) { XamlWriter.Save(obj, xamlDesignerSerializationManager); } return(xamlString.ToString()); }
internal ElementMarkupObject(object instance, XamlDesignerSerializationManager manager) { Debug.Assert(instance != null); _instance = instance; _context = new ElementObjectContext(this, null); _manager = manager; }
public static MarkupObject GetMarkupObjectFor(object instance, XamlDesignerSerializationManager manager) { if (instance == null || manager == null) { throw new ArgumentNullException(); } throw new NotImplementedException(); }
public bool ShouldSerializeChild(XamlDesignerSerializationManager manager) { bool shouldSerialize = false; if (manager != null) { shouldSerialize = (manager.XmlWriter == null); } return(shouldSerialize); }
public bool ShouldSerializeChild(XamlDesignerSerializationManager manager) { bool result = false; if (manager != null) { result = (manager.XmlWriter == null); } return(result); }
/// <summary> /// Save button callback that will save current layout /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void saveButton_Click(object sender, RoutedEventArgs e) { StringBuilder outstr = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; settings.NewLineOnAttributes = true; foreach (var child in controlCanvas.Children) { if (child.GetType().Name == "SimpleSwitch") { SimpleSwitch simpleSwitch = child as SimpleSwitch; Grid grid = simpleSwitch.Content as Grid; Image icon = grid.Children[1] as Image; icon.InputBindings[0].Command = null; } } XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings)); dsm.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(controlCanvas, dsm); string savedControls = outstr.ToString(); if (!Directory.Exists(@"layouts")) { Directory.CreateDirectory(@"layouts"); } File.WriteAllText(@"layouts/" + currentGrid + ".xaml", savedControls); foreach (var child in controlCanvas.Children) { if (child.GetType().Name == "SimpleSwitch") { SimpleSwitch simpleSwitch = child as SimpleSwitch; Grid grid = simpleSwitch.Content as Grid; Image icon = grid.Children[1] as Image; Binding commandBinding = new Binding("SwitchClickedCommand"); BindingOperations.SetBinding(icon.InputBindings[0], InputBinding.CommandProperty, commandBinding); } } Messenger.Default.Send("save", "saveObjects"); }
public static string SerializeObject(object instance) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(sb, settings)); dsm.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(instance, dsm); return(sb.ToString()); }
/// <summary> /// Saves a FixedDocument to a .xaml file at the target location. /// </summary> /// <param name="fileName"> /// The target location for the document. /// </param> /// <param name="documentSource"> /// An IDocumentPaginatorSource for the FixedDocument to be saved /// to disk. /// </param> private void SaveFile(string fileName, IDocumentPaginatorSource documentSource) { XmlTextWriter xmlWriter = null; TextWriter writer = null; Stream file = null; try { file = File.Create(fileName); writer = new StreamWriter(file); xmlWriter = new XmlTextWriter(writer); // Set serialization mode XamlDesignerSerializationManager xamlManager = new XamlDesignerSerializationManager(xmlWriter); // Serialize XamlWriter.Save(documentSource.DocumentPaginator.Source, xamlManager); } catch (Exception e) { string msg = string.Format("Error occurred during saving.{0}{0}{1}", Environment.NewLine, e.Message); MessageBox.Show(msg, "Recipe_07_10", MessageBoxButton.OK, MessageBoxImage.Error); } finally { if (xmlWriter != null) { xmlWriter.Close(); } if (writer != null) { writer.Close(); } if (file != null) { file.Close(); } } }
public static String XamlWriterFormatted(Object uie) { StringBuilder sb = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }; XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(sb, settings)); dsm.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(uie, dsm); return(sb.ToString()); }
public void WriteXml(XmlWriter writer) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineOnAttributes = true; settings.ConformanceLevel = ConformanceLevel.Fragment; XamlDesignerSerializationManager manager = new XamlDesignerSerializationManager(writer); manager.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(Content, manager); }
public static void Serialize(object toSerialize, Stream stream) { var settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineOnAttributes = true; settings.ConformanceLevel = ConformanceLevel.Fragment; var sb = new StringBuilder(); XmlWriter writer = XmlWriter.Create(stream, settings); var manager = new XamlDesignerSerializationManager(writer); manager.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(toSerialize, manager); writer.Flush(); }
public void Save(String name, object content) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; using (MemoryStream ms = new MemoryStream()) { XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(ms, settings)); dsm.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(content, dsm); Env.Current.Project.SetData("Schemas/" + name + "/xaml", ms); Name = name; Content = content; } }
public static string XMLSerialize(object toSerialize) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = false; settings.NewLineOnAttributes = true; settings.ConformanceLevel = ConformanceLevel.Fragment; StringBuilder sb = new StringBuilder(); XmlWriter writer = XmlWriter.Create(sb, settings); XamlDesignerSerializationManager manager = new XamlDesignerSerializationManager(writer); manager.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(toSerialize, manager); return(sb.ToString()); }
/// <summary> /// Loads the XAML. /// </summary> void LoadXAML() { StringBuilder outputString = new StringBuilder(); richTextBoxXaml.Document.Blocks.Clear(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; XamlDesignerSerializationManager designSerialization = new XamlDesignerSerializationManager(XmlWriter.Create(outputString, settings)); designSerialization.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(this.ImageSourceContent, designSerialization); richTextBoxXaml.Document.Blocks.Add(XAMLParser.GetFomattedXAML(outputString.ToString())); richTextBoxXaml.ScrollToHome(); }
public static string SerializeToStr([NotNull] this UIElement element) { var result = new StringBuilder(); var writer_settings = new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }; var writer = XmlWriter.Create(result, writer_settings); var manager = new XamlDesignerSerializationManager(writer) { XamlWriterMode = XamlWriterMode.Expression }; XamlWriter.Save(element, manager); return(result.ToString()); }
private void serializar() { string savedButton = System.Windows.Markup.XamlWriter.Save(contenedorVentanas); StringReader stringReader = new StringReader(savedButton); XmlReader xmlReader = XmlReader.Create(stringReader); StringBuilder outstr = new StringBuilder(); //this code need for right XML fomating XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings)); //this string need for turning on expression saving mode dsm.XamlWriterMode = XamlWriterMode.Expression; System.Windows.Markup.XamlWriter.Save(contenedorVentanas, dsm); String convertido = outstr.ToString(); StringReader stringReader2 = new StringReader(convertido); XmlReader xmlReader2 = XmlReader.Create(stringReader2); ContenedorVentanas readerLoadButton = (ContenedorVentanas)System.Windows.Markup.XamlReader.Load(xmlReader2); //System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ContenedorVentanas)); //using (var writer = new StreamWriter(@"d:\test.xml")) //{ // serializer.Serialize(writer, contenedorVentanas); //} //MemoryStream ms = new MemoryStream(); //BinaryFormatter bf = new BinaryFormatter(); //bf.Serialize(ms, this); /// data is the class i wanna serialize. //ms.Seek(0, 0); //StreamReader rdr = new StreamReader(ms); //string str = rdr.ReadToEnd(); //byte[] byteArray = Encoding.ASCII.GetBytes(str); //XamlSerializer xs = new XamlSerializer(); //xs.DiscoverAttachedProperties(typeof(AttachedProps)); //string text = xs.Serialize(this.nestedObject); }
public static string ToXaml(this FrameworkElement frameworkElement) { var sb = new StringBuilder(); var writer = XmlWriter.Create(sb, new XmlWriterSettings { Indent = true, ConformanceLevel = ConformanceLevel.Fragment, OmitXmlDeclaration = true }); var mgr = new XamlDesignerSerializationManager(writer) { XamlWriterMode = XamlWriterMode.Expression }; System.Windows.Markup.XamlWriter.Save(frameworkElement, mgr); return(sb.ToString()); }
public static string ToXaml <T>(T obj) { StringBuilder xamlStringBuilder = new StringBuilder(); using (XmlWriter xmlWriter = XmlWriter.Create(xamlStringBuilder, new XmlWriterSettings { OmitXmlDeclaration = true })) { XamlDesignerSerializationManager xamlDesignerSerializationManager = new XamlDesignerSerializationManager(xmlWriter) { XamlWriterMode = XamlWriterMode.Expression }; XamlWriter.Save(obj, xamlDesignerSerializationManager); } return(xamlStringBuilder.ToString()); }
public static string WriteToXaml(object o, bool indent) { StringBuilder sb = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); if (indent) { settings.Indent = true; settings.IndentChars = "\t"; } settings.OmitXmlDeclaration = true; settings.Encoding = Encoding.UTF8; XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager( XmlWriter.Create(sb, settings)); XamlWriter.Save(o, dsm); return(sb.ToString()); }
private static void SVGFont(string path, List <SolidColorBrush> brushes) { ResourceDictionary dictionary = new ResourceDictionary(); XDocument doc = XDocument.Load(path); IEnumerable <XElement> elements = doc.Descendants().Where(x => x.Name.LocalName.Contains("glyph")); int brushCount = 0; foreach (XElement element in elements) { SolidColorBrush brush = brushes[brushCount % brushes.Count]; XAttribute name = element.Attribute("glyph-name"); XAttribute data = element.Attribute("d"); if (name == null || data == null) { continue; } GeometryDrawing drawing = new GeometryDrawing { Brush = brush, Geometry = Geometry.Combine(Geometry.Empty, Geometry.Parse(data.Value), GeometryCombineMode.Union, new ScaleTransform(1, -1)) }; DrawingImage drawingImage = new DrawingImage(drawing); dictionary.Add(name.Value, drawingImage); brushCount++; } Console.WriteLine($"Successfully converted {dictionary.Count} characters"); if (brushCount < brushes.Count) { Console.WriteLine($"Too many colors were supplied. {brushes.Count - brushCount} colors were ignored"); } using (XmlWriter writer = XmlWriter.Create(Path.Combine(Path.GetDirectoryName(path), $"{Path.GetFileNameWithoutExtension(path)}.xaml"), new XmlWriterSettings { Indent = true })) { XamlDesignerSerializationManager sm = new XamlDesignerSerializationManager(writer); sm.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(dictionary, writer); } }
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/65a2064f-2d6a-4ecc-8076-60c72cb7070d/wpf-c-save-controls-created-at-runtime?forum=wpf // check the above link for example code of how to save user controls as XML serialization private void SaveCounters() { StringBuilder sb = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; settings.NewLineOnAttributes = true; XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(sb, settings)); dsm.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(CounterStackPanel, dsm); string savedControls = sb.ToString(); File.WriteAllText(@"LastSession.xaml", savedControls); }
public string SerializeBrushToXml() { var settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineOnAttributes = true; settings.ConformanceLevel = ConformanceLevel.Fragment; var sb = new StringBuilder(); XmlWriter writer = XmlWriter.Create(sb, settings); var manager = new XamlDesignerSerializationManager(writer); manager.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(Brush, manager); return(sb.ToString()); }
public static string XamlToString <T>(T Object) where T : FrameworkElement { var sb = new StringBuilder(); var writer = XmlWriter.Create(sb, new XmlWriterSettings { Indent = true, ConformanceLevel = ConformanceLevel.Fragment, OmitXmlDeclaration = true, NamespaceHandling = NamespaceHandling.OmitDuplicates, }); var mgr = new XamlDesignerSerializationManager(writer); // HERE BE MAGIC!!! mgr.XamlWriterMode = XamlWriterMode.Expression; // THERE WERE MAGIC!!! System.Windows.Markup.XamlWriter.Save(Object, mgr); return(sb.ToString()); }
public static string GetXaml(object obj, string indent) { var sb = new StringBuilder(); var writer = System.Xml.XmlWriter.Create(sb, new System.Xml.XmlWriterSettings { Indent = true, IndentChars = indent, ConformanceLevel = ConformanceLevel.Fragment, OmitXmlDeclaration = true }); var mgr = new XamlDesignerSerializationManager(writer) { XamlWriterMode = XamlWriterMode.Expression }; System.Windows.Markup.XamlWriter.Save(obj, mgr); return(sb.ToString()); }