我需要迭代从IMapView获得的Windows::ApplicationModel::Store::LicenseInformation。它应该与标准的for_each一起工作,bud不能使用C++/CX,只能使用WRL。
我现在只有ComPtr<IMapView<HSTRING, ProductLicense*>> productLicences;,如何才能将productLicences的内容放到一些标准的集合中呢?
谢谢
发布于 2014-02-28 17:25:17
我只是在一个不同的MS平台上遇到了同样的问题,并且遇到了这个没有答案的问题。也许为时已晚,但我就是这样解决的。关键是IMapView继承自IIterable,因此您需要获得IIterable接口(使用QueryInterface)并获得一个Iterator。下面的代码是从我的平台到您的平台的一个调整,所以它可能不是100%正确的,但是它提供了使用WRL进行迭代的关键元素。
首先,获取IIterable接口和一个有效的Iterator。
ComPtr<IMapView<IKeyValuePair<HSTRING,ProductLicense*>> map;
THROW_IF_FAILED(licenseInfo->get_ProductLicenses(&map));
unsigned int mapSize = 0;
THROW_IF_FAILED(map->get_Size(&mapSize));
wprintf(L"map size %i\n", mapSize);
ComPtr<IIterable<IKeyValuePair<HSTRING,ProductLicense*>*>> iterable;
panelMap.As(&iterable);
ComPtr<IIterator<IKeyValuePair<HSTRING,ProductLicense*>*>> iterator;现在,您拥有了执行迭代所需的所有信息。将迭代器设置为Map的第一个元素,然后开始迭代。下面的代码说明了迭代过程。
THROW_IF_FAILED(iterable->First(&iterator));
boolean hasCurrent = false;
THROW_IF_FAILED(iterator->get_HasCurrent(&hasCurrent));
while(hasCurrent)
{
ComPtr<IKeyValuePair<HSTRING,ProductLicense*>> pair;
THROW_IF_FAILED(iterator->get_Current(&pair));
HString key;
THROW_IF_FAILED(pair->get_Key(&key));
ComPtr<IProductLicense> license;
THROW_IF_FAILED(pair->get_Value(&license));
THROW_IF_FAILED(iterator->MoveNext(&hasCurrent));
}在大多数支持WRL的windows平台上,这应该都能很好地工作,只需对某些方法进行少量扩展(例如,在我的平台迭代器->Current(.)中)有稍微不同的名字)。
https://stackoverflow.com/questions/18331939
复制相似问题