Dynamic Xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sys = System; | |
using SysIO = System.IO; | |
using SysWinMarkup = System.Windows.Markup; | |
using SysXml = System.Xml; | |
using SysXmlLinq = System.Xml.Linq; | |
public static class XamlUtils { | |
public static object GetXamlObject(string source) { | |
SysXml.XmlDocument _xmlDocument = new SysXml.XmlDocument(); | |
_xmlDocument.LoadXml(source); | |
return Xaml.GetXamlObject(_xmlDocument); | |
} | |
public static object GetXamlObject(SysXml.XmlDocument xmlDocument) { | |
const string xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"; | |
const string xmlns_x = "http://schemas.microsoft.com/winfx/2006/xaml"; | |
if(string.IsNullOrEmpty(xmlDocument.DocumentElement.GetAttribute("xmlns"))) { | |
xmlDocument.DocumentElement.SetAttribute("xmlns", xmlns); | |
} | |
if(string.IsNullOrEmpty(xmlDocument.DocumentElement.GetAttribute("xmlns:x"))) { | |
xmlDocument.DocumentElement.SetAttribute("xmlns:x", xmlns_x); | |
} | |
SysIO.TextReader _stringReader = new SysIO.StringReader(xmlDocument.OuterXml); | |
SysXml.XmlReader _xmlReader = new SysXml.XmlTextReader(_stringReader); | |
return SysWinMarkup.XamlReader.Load(_xmlReader); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
System.Windows.Controls.Grid _gridObject = XamlUtils.GetXamlObject("<grid><textblock>ok</textblock></grid>") as System.Windows.Controls.Grid; | |
this.Content = _gridObject; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment