【问题标题】:Convert Markup Extension Shorthand Syntax to Full Xml Form将标记扩展简写语法转换为完整的 Xml 形式
【发布时间】:2014-02-26 17:30:07
【问题描述】:

我有 XAML 字符串,例如:

    <Button Content={Binding Name} Style={StaticResource ButtonStyle}/>

如何将所有大括号转换为代码中的元素,例如:

    <Button>
      <Button.Content>
         <Binding Path="Name">
      </Button.Content>
      <Button.Style>
         <StaticResource ResourceKey="ButtonStyle"/>
      </Button.Style>
    </Button>

【问题讨论】:

  • 如果您解释了您想要实现的目标,那将会有所帮助,因此我们知道您为什么要进行转换。
  • 我认为这是一个有趣的话题,但是您能展示一下您迄今为止为自己解决问题所做的尝试吗?标记扩展语法由 Microsoft 明确定义,那么您特别纠结的是什么?
  • 顺便说一下,您的示例说明了所需的转换,但它包含一些语法错误。
  • 我需要创建解析器来读取从 Zam3D 生成的资源字典并更改 xaml 文件(更改结构)以在我的程序中打开它(作为 xml)。将 xml 序列化为原生对象(不是控件或 UIElements)的程序
  • 我需要将数据读取为 XmlElement 或 XElement 的方法,但我无法正确读取大括号之间的数据。

标签: c# wpf xaml markup-extensions


【解决方案1】:

您可以使用XamlReaderXamlWriter,我认为(!)默认情况下不会保存速记品种。见http://msdn.microsoft.com/en-us/library/system.windows.markup.xamlreader(v=vs.110).aspx

包括一个例子

// Create the Button.
Button originalButton = new Button();
originalButton.Height = 50;
originalButton.Width = 100;
originalButton.Background = Brushes.AliceBlue;
originalButton.Content = "Click Me";

// Save the Button to a string. 
string savedButton = XamlWriter.Save(originalButton);

// Load the button
StringReader stringReader = new StringReader(savedButton);
XmlReader xmlReader = XmlReader.Create(stringReader);
Button readerLoadButton = (Button)XamlReader.Load(xmlReader);

上面保存的按钮看起来像

 <Button Background="#FFF0F8FF" Width="100" Height="50" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">Click Me</Button>

【讨论】:

  • 这是迄今为止对这个问题的唯一真正答案。
猜你喜欢
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
相关资源
最近更新 更多