调用 XamlReader.Load 引发 XamlParseException
我正在使用 .net 4 的 System.Windows.Markup.XamlReader - 只是作为一种教育练习 - 我不断遇到同样的问题:使用 XamlReader.Load 加载 xaml如果根对象定义了
x:Class
,code> 会抛出 XamlParseException
,但如果没有,则会成功解析并加载节点。
这是我正在尝试的代码:
using System.Windows;
using System.Xaml;
using XamlReader = System.Windows.Markup.XamlReader;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Load up UserControl1.xaml from the solution
var reader = new XamlXmlReader(@"../../UserControl1.xaml", XamlReader.GetWpfSchemaContext());
var userControl = XamlReader.Load(reader) as UserControl1;
// Do something with userControl...
}
}
}
我已经尝试直接从包含 xaml 的字符串中尝试 XamlReader.Parse
,结果相同:仅在未定义 x:Class 声明的情况下才有效。
删除 x:Class
声明似乎不是一个好的选择,因为这样我就会丢失代码隐藏,特别是对 InitalizeComponent()
的调用
异常详细信息:
< code>'指定的类名'WpfApplication2.UserControl1'与实际根实例类型'System.Windows.Controls.UserControl'不匹配。删除 Class 指令或通过 XamlObjectWriterSettings.RootObjectInstance 提供一个实例。'
...但我不知道如何(在哪里)设置 XamlObjectWriterSettings.RootObjectInstance
(或者确实,如果需要的话) ?)
有任何线索吗?
I'm playing around with .net 4's System.Windows.Markup.XamlReader
- just as an education exercise - and I keep bumping into the same problem: Loading xaml with XamlReader.Load
throws a XamlParseException
if the root object defines an x:Class
, but successfully parses and loads the node if not.
Here's the code I'm trying:
using System.Windows;
using System.Xaml;
using XamlReader = System.Windows.Markup.XamlReader;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Load up UserControl1.xaml from the solution
var reader = new XamlXmlReader(@"../../UserControl1.xaml", XamlReader.GetWpfSchemaContext());
var userControl = XamlReader.Load(reader) as UserControl1;
// Do something with userControl...
}
}
}
I've tried XamlReader.Parse
directly from a string holding the xaml with the same result: only works if no x:Class declaration is defined.
Removing the x:Class
declaration doesn't seem like a good option, because then I lose the code-behind, specifically the call to InitalizeComponent()
The exception detail:
'Specified class name 'WpfApplication2.UserControl1' doesn't match actual root instance type 'System.Windows.Controls.UserControl'. Remove the Class directive or provide an instance via XamlObjectWriterSettings.RootObjectInstance.'
...but I don't know how (where) to set XamlObjectWriterSettings.RootObjectInstance
(or indeed, if that's required?)
Any clues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XamlReader 是一个解析器,而不是编译器,因此不支持代码隐藏。如果您需要将代码与动态加载的 XAML 关联,您可以执行一些操作,例如将其包装到其他地方定义的控件中,您可以在 XAML 中使用该控件的实例,或者在读取 XAML 后连接代码(即事件处理程序) ) 到结果对象中的元素。
XamlReader is a parser, not a compiler, so doesn't support code-behind. If you need to associate code with your dynamically loaded XAML you can do something like wrapping it up into a control defined elsewhere that you can use an instance of in the XAML or, after reading in the XAML, connect up the code (i.e. event handlers) to elements in the resulting object.
您不能在动态 XAML 中使用 x:Class。相反,您可以在加载 XAML 之后挂钩事件。请查看此链接
通过运行时加载 XAML XML?
You can't use x:Class in dynamic XAML. Instead what you can do is you can hook events after the loading XAML. please have a look at this link
Loading XAML XML through runtime?