Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用列表<;T>;或字典<;T、 T2>;在C#WinRT组件中_C#_Winapi_Windows 8_Windows Runtime_Microsoft Metro - Fatal编程技术网

如何使用列表<;T>;或字典<;T、 T2>;在C#WinRT组件中

如何使用列表<;T>;或字典<;T、 T2>;在C#WinRT组件中,c#,winapi,windows-8,windows-runtime,microsoft-metro,C#,Winapi,Windows 8,Windows Runtime,Microsoft Metro,我正在尝试创建一个C#WinRT组件,用于metro风格的应用程序(win8),但在投影类型方面遇到了问题 IVector和IMap数据类型似乎由于其保护级别而无法访问 我的示例WinMD库有一个类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.Foundation.Collecti

我正在尝试创建一个C#WinRT组件,用于metro风格的应用程序(win8),但在投影类型方面遇到了问题

IVector和IMap数据类型似乎由于其保护级别而无法访问

我的示例WinMD库有一个类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation.Collections;

namespace ClassLibrary1
{
    public sealed class Class1
    {
        public IVector<string> foo()
        {
            return new List<string>();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Windows。
命名空间类库1
{
公共密封类别1
{
公营机构
{
返回新列表();
}
}
}
我发现以下编译器错误:

Inconsistent accessibility: return type 'Windows.Foundation.Collections.IVector<string>' is less accessible than method 'ClassLibrary1.Class1.foo()'

'Windows.Foundation.Collections.IVector<string>' is inaccessible due to its protection level
<代码>不可访问性:返回类型“Windows .Fun.Posial.IVoice”比方法“CalpBaseLaby1.Cult1.FoW()”更不易访问 “Windows .Fun.Posial.IVACT”由于其保护级别是不可访问的 我做错了什么

编辑:

啊哈

事实证明,我不应该直接使用WinRT类型名称,而是使用它们的翻译后的.NET名称

正确的代码如下所示:

namespace ClassLibrary1
{
    public sealed class Class1
    {
        public IList<string> foo()
        {
            return new List<string>();
        }
    }
}
命名空间类库1
{
公共密封类别1
{
公共图书馆
{
返回新列表();
}
}
}
交互式
作为
IEnumerable
投影到.NET,
IVector
作为
IList
投影,
IMap
作为
IDictionary
投影到.NET。投影是双向的-既用于消费,也用于创作-因此您应该在.NET代码中始终使用这些接口的投影版本。当您将一个成员声明为返回
IList
时,它将从WinRT角度(例如从C++/CX)显示为
IVector
。类似地,如果在类上实现
IDictionary
,它将在C++/CX中显示为实现
IMap


如果我没记错的话,您应该看到所有类型都已经映射到了.NET项目的对象浏览器中。

这是一个WinRT组件,它的核心使用本机API。所以它需要项目列表->IVector。有关更多详细信息,请参阅。映射表大约在页面的一半。这并不能真正解释我看到的保护级别错误。虽然不是对我问题的直接回答,但您为我指出了正确的方向。我已经更新了我的帖子。干杯我想我还不够清楚。原始的
IVector
和其他东西都在.winmd中(因此出现了这些保护级别错误),您不应该直接使用它,而是始终使用所描述的替换。我将为通过搜索来到这里的其他人更新答案。来自MSDN:and的一些相关链接。请注意,在实践中,事情比这个答案所暗示的要复杂一些:您在C++/CX中创建一个
向量
,然后在C#中以
IList
的形式获取项目。或者在C++中创建一个
列表
,该列表在C++/CX中被投影为
IVector
。您不能创建
IVector
IList