在c#中加载xaml文件,获取具有指定名称的元素,添加按钮并保存文件
我有一个XAML文件,文件名为在c#中加载xaml文件,获取具有指定名称的元素,添加按钮并保存文件,c#,wpf,xaml,C#,Wpf,Xaml,我有一个XAML文件,文件名为MainWindow.XAML: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height
MainWindow.XAML
:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="Content">
</Grid>
</Window>
但我无法获取名为
内容的网格元素,为什么?使用System.Windows.Markup.XamlReader
从流加载到文件中
XamlReader类
读取XAML输入并使用WPF默认值创建对象图
XAML读取器和关联的XAML对象编写器
必须将返回对象强制转换为根类型才能使用它。在此exmaple中,根元素是
using(var fs = new FileStream("MainWindow.xaml"))
{
Window page = (Window)System.Windows.Markup.XamlReader.Load(fs);
}
发布另一个答案,因为第一个答案很有用,但不能立即满足您的要求
你给我们的文件无效。它没有关闭窗口
标记
无法获取网格元素的原因是您没有提供要使用的命名空间。由于网格元素没有名称空间前缀,我们可以假设它的名称空间是文档的默认名称空间<代码>http://schemas.microsoft.com/winfx/2006/xaml/presentation
我如何知道文档的默认值是多少?由于文档中的第2行:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
编辑
Element()
方法采用元素的xml名称-在本例中,您需要Grid
。然后您必须检查名称
您可能希望简化代码。由于这是XAML,因此可以对格式进行假设。例如,窗口将只有一个子元素
这是我的测试代码。这里有各种各样的隐式操作符,它们会使+
操作符编译,尽管类型很奇怪。别担心:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Linq;
namespace XamlLoad
{
class Program
{
static void Main(string[] args)
{
string file = @"<Window x:Class=""WpfApplication1.MainWindow""
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
Title=""MainWindow"" Height=""350"" Width=""525"">
<Grid x:Name=""Content"">
</Grid>
</Window>";
var doc = XDocument.Load(new StringReader(file));
XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml";
var gridElement = doc.Root.Elements(xmlns + "Grid").Where(p => p.Attribute(x + "Name") != null && p.Attribute(x + "Name").Value == "Content");
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
使用System.Xml.Linq;
名称空间XamlLoad
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串文件=@“
";
var doc=XDocument.Load(新的StringReader(文件));
XXXmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XX=”http://schemas.microsoft.com/winfx/2006/xaml";
var gridElement=doc.Root.Elements(xmlns+“Grid”)。其中(p=>p.Attribute(x+“Name”)!=null&&p.Attribute(x+“Name”)。值==“Content”);
}
}
}
您需要类似于元素(xmlns+“Grid”)的东西。其中(e=>e.Attributes.Any(a=>a.value==“Content”)代码>@user2400938很高兴听到你让它工作了。我使用了XDocument.Load的TextReader重载,以便将示例文件(字符串)和加载代码放在单个文件中。在没有错误的情况下,我只能假设这是因为您使用的是非STA单元线程来完成这项工作?检查发生的异常的内部异常。如果做不到这一点,给我一些更好的工作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Linq;
namespace XamlLoad
{
class Program
{
static void Main(string[] args)
{
string file = @"<Window x:Class=""WpfApplication1.MainWindow""
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
Title=""MainWindow"" Height=""350"" Width=""525"">
<Grid x:Name=""Content"">
</Grid>
</Window>";
var doc = XDocument.Load(new StringReader(file));
XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml";
var gridElement = doc.Root.Elements(xmlns + "Grid").Where(p => p.Attribute(x + "Name") != null && p.Attribute(x + "Name").Value == "Content");
}
}
}