Issue with x:TypeArguments and generic List class in XAML

僤鯓⒐⒋嵵緔 提交于 2020-01-02 02:21:05

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6053379/issue-with-xtypearguments-and-generic-list-class-in-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!