如何修复 WinRT 中 IDL 文件的 'the arguments to the parameterized interface are not valid' 错误?

How to fix 'the arguments to the parameterized interface are not valid' error of IDL file in WinRT?

我在尝试为我的 Windows 运行时组件 class.

编写 IDL 文件时遇到 "the arguments to the parameterized interface are not valid" 错误

我的 header 中的 RunAsync() 函数 returns winrt::Windows::Foundation::IAsyncOperation 我将它翻译成 winrt.Windows.Foundation.IAsyncOperation 因为 https://docs.microsoft.com/en-us/uwp/winrt-cref/winrt-type-system 指出 UInt32 是 "fundamental type" 和“[WinRT 基本类型] 允许出现在参数化类型的参数列表中”。

//ConnectTask.idl
namespace NOVAShared
{
    [default_interface]
    runtimeclass ConnectTask
    {
        ConnectTask();
        winrt.Windows.Foundation.IAsyncOperation<UInt32> RunAsync();
    };
}
//ConnectTask.h
namespace winrt::NOVAShared::implementation
{
    struct ConnectTask : ConnectTaskT<ConnectTask>
    {
        ConnectTask() = default;

        static winrt::Windows::Foundation::IAsyncOperation<uint32_t> RunAsync();
    };
}

我的语法有误吗?我发现了一些 IDL 文件的随机示例,它似乎是正确的...

MIDL 编译器的错误消息具有一定的误导性。当你编译下面的 IDL 文件时

namespace NS
{
    runtimeclass MyType
    {
        foo<UInt32> bar();
    }
}

您将收到此错误消息:

error MIDL5023: [msg]the arguments to the parameterized interface are not valid [context]: foo

但是,无效的不是参数。这是未知的参数化类型 (foo)。在你的例子中是 winrt.Windows.Foundation.IAsyncOperation。不存在具有该名称的类型。 Windows 运行时类型名称改为 Windows.Foundation.IAsyncOperation(投影到 C++/WinRT 中的 winrt 命名空间,即 winrt::Windows::Foundation::IAsyncOperation)。

要解决此问题,请使用以下 IDL 文件:

//ConnectTask.idl
namespace NOVAShared
{
    [default_interface]
    runtimeclass ConnectTask
    {
        ConnectTask();
        Windows.Foundation.IAsyncOperation<UInt32> RunAsync();
    };
}

请注意,如果您想要静态 class 成员,则必须在 IDL 中使用 static 关键字。