gpt4 book ai didi

c# - 如何在 C# WinRT 组件中使用 List 或 Dictionary

转载 作者:太空狗 更新时间:2023-10-29 23:09:56 31 4
gpt4 key购买 nike

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

似乎 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>();
}
}
}

我收到以下编译器错误:

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

我做错了什么?

编辑:

啊哈!

原来我不应该直接使用 WinRT 类型名称,而是使用它们翻译后的 .NET 名称。

正确的代码是这样的:

namespace ClassLibrary1
{
public sealed class Class1
{
public IList<string> foo()
{
return new List<string>();
}
}
}

最佳答案

IIterable<T>转换到 .NET 为 IEnumerable<T> , IVector<T>预计为 IList<T> , 和 IMap<T>预计为 IDictionary<T> .投影有两种方式 - 用于消费和创作 - 因此您应该始终在 .NET 代码中使用这些接口(interface)的投影版本。当您声明成员返回时 IList<T> ,它将显示为 IVector<T>从 WinRT 的角度(例如从 C++/CX)。同样,如果您实现 IDictionary在你的类里面,它会显示为实现 IMap在 C++/CX 中。

如果我没记错的话,您应该看到所有类型都已经映射,就像您在 .NET 项目中使用对象浏览器时应该看到的那样。

关于c# - 如何在 C# WinRT 组件中使用 List<T> 或 Dictionary<T,T2>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9285752/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com