当我使用WRL创建一个winrt组件时,问题是我只能使用ABI::Windows::xxx命名空间,而不能在WRL中使用Windows::UI::Xaml::Media::Imaging命名空间。
那么,如何创建一个内置winrt组件作为返回值呢?
// idl
import "inspectable.idl";
import "Windows.Foundation.idl";
import "Windows.UI.Xaml.Media.Imaging.idl";
namespace Decoder
{
interface IPhotoDecoder;
runtimeclass PhotoDecoder;
interface IPhotoDecoder : IInspectable
{
HRESULT Decode([in] int width, [in] int height, [out, retval] Windows.UI.Xaml.Media.Imaging.BitmapImage **ppBitmapImage);
}
[version(COMPONENT_VERSION), activatable(COMPONENT_VERSION)]
runtimeclass PhotoDecoder
{
[default] interface IPhotoDecoder;
}
}
// cpp
using namespace Microsoft::WRL;
using namespace Windows::Foundation;
using namespace ABI::Windows::UI::Xaml::Media::Imaging;
namespace ABI
{
namespace Decoder
{
class PhotoDecoder: public RuntimeClass<IPhotoDecoder>
{
InspectableClass(L"Decoder.PhotoDecoder", BaseTrust)
public:
PhotoDecoder()
{
}
HRESULT __stdcall Decode(_In_ int width, _In_ int height, _Out_ IBitmapImage **ppBitmapImage)
{
// How to create Windows.UI.Xaml.Media.Imaging.BitmapImage without using Windows::UI::Xaml::Media::Imaging
}
};
ActivatableClass(PhotoDecoder);
}
}发布于 2012-10-03 01:16:16
有两组名称空间:
对以全局命名空间为根的Windows::Foundation)
ABI命名空间为根的ABI::Windows::Foundation))进行
每个文件的内容都是“相同的”。例如,Windows::Foundation::IUriRuntimeClass与ABI::Windows::Foundation::IUriRuntimeClass命名相同的接口。
那么,为什么会有两组名称空间呢?以全局命名空间为根的命名空间保留供C++/CX使用:它在这些命名空间中生成其运行时类的投影。当您使用WRL时,您将始终使用以ABI名称空间为根的名称空间(这些名称是“未投影”的名称,也就是说,它们正是存在于ABI层的名称)。
运行时类以两种方式之一实例化(“激活”)。如果该类型是默认可构造类型,则可以通过调用RoActivateInstance默认构造该类型。如果一个类型声明了其他构造函数,则可以通过调用RoGetActivationFactory获得运行时类型的激活工厂来调用这些构造函数。例如,您可以默认构造一个BitmapImage,如下所示:
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::UI::Xaml::Media::Imaging;
HStringReference classId(RuntimeClass_Windows_UI_Xaml_Media_Imaging_BitmapImage);
ComPtr<IInspectable> inspectable;
if (FAILED(RoActivateInstance(classId.Get(), inspectable.GetAddressOf())))
{
// Handle failure
}
ComPtr<IBitmapImage> bitmapImage;
if (FAILED(inspectable.As(&bitmapImage)))
{
// Handle failure
}WRL还有一个有用的函数模板Windows::Foundation::ActivateInstance,它调用RoActivateInstance并对所需的目标接口执行QueryInterface:
using namespace Windows::Foundation;
ComPtr<IBitmapImage> bitmapImage;
if (FAILED(ActivateInstance(classId.Get(), bitmapImage.GetAddressOf())))
{
// Handle failure
}https://stackoverflow.com/questions/12687502
复制相似问题