Any way to extract underlying Xaml?

别说谁变了你拦得住时间么 提交于 2019-12-22 14:47:33

问题


Is there anyway to extract the underlying xaml from a control.

IE. I have a textbox named fooBox. Can I get xaml back from the textbox at runtime that represents the textbox?


回答1:


This shows you the full lifecycle (from control to XAML back to control). As you can see,

string s = XamlWriter.Save(value);

is the interesting part you might care about.

    /// <summary>
    /// Clones a given UIElement.  Please note that any events, animations, etc
    /// on the source item may not carry over to the cloned object.
    /// </summary>
    /// <param name="value">UIElement to clone.</param>
    /// <returns>A shallow clone of the source element.</returns>
    public static UIElement CloneUIElement(UIElement value)
    {
        if (value == null)
        {
            return null;
        }

        string s = XamlWriter.Save(value);
        StringReader stringReader = new StringReader(s);
        XmlReader xmlReader = XmlTextReader.Create(stringReader, new XmlReaderSettings());
        return (UIElement)XamlReader.Load(xmlReader);
    }



回答2:


For Silverlight, I ran across this blog post by Rob Relyea that referred to a Silverlight xaml serializer created by David Poll. Huge kudos to David Poll. (Downloads Page).

Usage

UiXamlSerializer uxs = new UiXamlSerializer();
string text = uxs.Serialize(this.gridToSerialize);



回答3:


Yes - Josh Smith does this in his Mole tool: http://www.codeproject.com/KB/WPF/MoleForWPF.aspx



来源:https://stackoverflow.com/questions/783497/any-way-to-extract-underlying-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!