如何解析XAML代码? [英] How to parse a XAML code?

查看:104
本文介绍了如何解析XAML代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

在我的应用程序中,我想阅读一些XAML代码并创建一些它描述的UI控件.我使用了以下代码:

Dear all,

In my application I would like to read some XAML code and create some UI controls described by it. I used the following code:

UIControlClass uiControl = (UIControlClass)System.Windows.Markup.XamlReader.Load(streamContainingTheXAMLcode);


并且它对于诸如Buttons和Grids之类的控件工作正常,但是当我尝试创建整个窗口(顺便说一句,它是Window1:Window类)XamlParseException时说:


and it worked fine for controls like Buttons and Grids, for example, but when I tried to create the whole window (which by the way is of class Window1 : Window) XamlParseException which says:


XML名称空间"http://schemas.microsoft.com/winfx/2006/xaml"名称空间中不存在"Class"属性.行"4"位置"5"."

"''Class'' attribute does not exist in XML namespace ''http://schemas.microsoft.com/winfx/2006/xaml'' namespace. Line ''4'' Position ''5''."


被扔了.如果我删除了决定我的窗口类的XAML代码的一部分,在这种情况下


was thrown. If I remove part of XAML code which determines Class of my window, in this particular case

x:Class="SkinFriendlyApplication1.Window1"


不会引发异常,并且上面的C#代码可以工作,但是创建的窗口属于Class Window,并且我失去了所有的Window1功能.

有什么方法可以向XamlReader解释什么是"Class"属性",如果是,怎么做?
另外,如果还有其他三种方法来读取XAML代码并基于该方法创建控件,那也是令人满意的解决方案.

在此先感谢您.


the exception is not thrown and the above C# code works, but the window created is of Class Window and I lose all my Window1 functionality.

Is there any way to explain to XamlReader what "''Class'' attribute" is, and if so, how?
Also, if three is some other way to read XAML code and create controls based on it, that would be satisfactory solution, too.

Thanks in advance.

推荐答案

您不能使用XamlReader.Load加载与文件背后的代码关联的Xaml.删除文件后面的代码,使用XamlReader.Load获取一个Window 对象,然后在运行时添加所需的事件处理程序(这与在您的Window 派生类中覆盖virtual 方法相对).
或者,使用msbuild在运行时编译Xaml/cs文件,然后通过反射将其加载.
You cannot use XamlReader.Load to load Xaml that''s associated with a code behind file. Remove the code behind file, use XamlReader.Load to get a Window object and then add the required event handlers at runtime (as opposed to overriding virtual methods in your Window derived class).

Alternatively use msbuild to compile the Xaml/cs files at runtime and then load it via reflection.


这篇关于如何解析XAML代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆