有没有办法在运行时以编程方式在 c# 中操作 xaml 文件?

Is there a way to manipulate xaml file in c# programmatically at runtime?

我的项目的目的是操纵 xaml 代码,以便将现有的 Xaml UWP 代码转换为不同平台的 Xaml 代码。任何帮助将不胜感激。

StreamReader stream = new StreamReader("Path to xaml file");
FrameworkElement root = XamlReader.Load(stream.BaseStream) as FrameworkElement;

这里的问题是 xaml 文件包含 x:class,显然无法在运行时加载。那么有没有更好的方法来操作属性。

您可以组合 XamlReader.Load 和 XamlServices.Parse 以编程方式加载和解析 xaml 文件:

https://msdn.microsoft.com/en-us/library/system.xaml.xamlservices.parse(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/ms590388(v=vs.110).aspx

How to parse a xaml file/string

https://github.com/dotnet/docs/blob/master/docs/framework/xaml-services/xamlservices-class-and-basic-xaml-reading-or-writing.md

https://blogs.msdn.microsoft.com/ashish/2007/08/14/dynamically-loading-xaml/