gpt4 book ai didi

c# - 添加 x :Key attribute using Linq to Xml

转载 作者:行者123 更新时间:2023-11-30 15:38:27 24 4
gpt4 key购买 nike

我正在尝试使用 Linq to XML 在某些 XAML 中设置 x:Key,以便我可以将值转换器添加到数据模板的资源字典中:

 XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace local = "clr-namespace:App,assembly=App";
XElement dt = new XElement(xmlns + "DataTemplate",
new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
new XAttribute(XNamespace.Xmlns + "local", "clr-namespace:App,assembly=App"),
new XElement(xmlns + "DataTemplate.Resources",
new XElement(local + "MyConverter",
new XAttribute("x:Key", "myConverter"))));

然而,这引发了一个异常,提示“:”在属性名称中是不允许的。使用另一个 XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml" 并编写 x + "Key" 也不起作用 - 它给出 p3:Key

有没有办法在 XAttribute 名称中包含一个冒号?

最佳答案

    XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace local = "clr-namespace:App,assembly=App";
XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml";

XElement dt = new XElement(xmlns + "DataTemplate",
new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
new XAttribute(XNamespace.Xmlns + "local", "clr-namespace:App,assembly=App"),
new XElement(xmlns + "DataTemplate.Resources",
new XElement(local + "MyConverter",
new XAttribute(x + "Key", "myConverter"))));

Console.WriteLine(dt);

输出:

<DataTemplate xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:App,assembly=App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<DataTemplate.Resources>
<local:MyConverter x:Key="myConverter" />
</DataTemplate.Resources>
</DataTemplate>

关于c# - 添加 x :Key attribute using Linq to Xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11428823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com