XAML 中的 x:TypeArguments 和泛型 List 类存在问题
我为松散的 XAML 文件创建了以下标记。
<StackPanel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib">
<scg:List x:TypeArguments="sys:String">
HelloWorld
</scg:List>
</StackPanel>
但当我在 IE 中运行松散的 XAML 时,出现此错误:
XML 命名空间“clr-namespace:System.Collections.Generic; assembly=mscorlib”中不存在标签“List”。第 '7' 行位置 2'。
如您所知,XAML 中的泛型是 XAML 2009 中的一项功能,并且大部分只能在松散的 XAML 文件中使用。但上面的代码不起作用。
有任何线索说明为什么会发生此错误以及如何纠正该问题吗?提前致谢。
I created the following markup for a loose XAML file.
<StackPanel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib">
<scg:List x:TypeArguments="sys:String">
HelloWorld
</scg:List>
</StackPanel>
But I get this error when I run the loose XAML in IE:
The tag 'List' does not exist in XML namespace 'clr-namespace:System.Collections.Generic;assembly=mscorlib'. Line '7' Position 2'.
As you would know, generics in XAML are a feature in XAML 2009 and can work for the most part only in loose XAML files. But the above code doesnt work.
Any clue why this error occurred and how to rectify the issue? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚使用 Internet Explorer 9 测试了您的示例。IE9 使用
PresentationHost。我在我的系统(Windows 7 SP1 x64)上运行了 .exe
来呈现内容,并通过检查实际加载了哪些程序集,我确认它使用了不支持 XAML 2009 的 v3.0 框架。文档描述,对于 XBAP,它选择要加载的框架版本,因此它显然能够使用 v4.0 框架,该框架确实支持松散 XAML 的 XAML 2009。然而,不幸的是,文档没有说明它将选择哪个版本的框架用于松散的 XAML,而不是 XBAP。
根据经验,至少通过您的示例,我可以确认
PresentationHost.exe
选择 v3.0 框架。我找不到任何方法来覆盖此选择,例如通过以某种方式注释 XAML。I've just tested your sample with Internet Explorer 9. IE9 uses
PresentationHost.exe
to render the content and on my system (Windows 7 SP1 x64), and by examining which assemblies are actually loaded I confirmed that it uses the v3.0 framework which does not support XAML 2009.The documentation describes that for XBAPs it chooses which framework version to load and so it is clearly capable of using the v4.0 framework which does support XAML 2009 for loose XAML. However, the documentation unfortunately does not say which version of the framework it will choose for loose XAML as opposed to XBAPs.
Empirically, at least with your sample, I can confirm that
PresentationHost.exe
chooses the v3.0 framework. I cannot find any way to override this selection, for example by annotating the XAML somehow.问题已经解决。我需要在标记中包含以下命名空间映射以启用泛型。
xmlns:v4="http://schemas.microsoft.com/netfx/2009/xaml/presentation"
现在它工作正常。
The issue has been resolved. I needed to include the following namespace mapping in the markup to enable use of generics.
xmlns:v4="http://schemas.microsoft.com/netfx/2009/xaml/presentation"
Now it works fine.