【问题标题】:How to include System in C++/WinRT console application如何在 C++/WinRT 控制台应用程序中包含系统
【发布时间】:2021-09-11 00:40:30
【问题描述】:

使用 Visual Studio 2019 16.10.2 如何在 C++/WinRT 控制台程序中包含 .NET 组件?

The indexOf 方法 of IVector 需要来自 Systema UInt32 struct

System 在这种情况下如何使用?尝试使用命名空间会导致

"System' : a namespace with this name does not exist"

This has been covered already on SO,但仅适用于 C++/CLI 应用程序,不适用于 C++/WinRT 控制台应用程序的上下文。该帖子中提供的解决方案也不起作用。

#include "pch.h"

#include <Windows.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>

using winrt::Windows::Foundation::Collections::IVector;
using namespace System;

int main()
{
    IVector<int> foo;
    foo.Append(1);
    UInt32 bar;
    foo.indexOf(1, bar);

    return EXIT_SUCCESS;
}

在这种情况下,pch.h 为空。

【问题讨论】:

    标签: c++ .net visual-studio windows-runtime c++-winrt


    【解决方案1】:

    当您从右上角的 documentation 的语言下拉列表中选择适当的语言 (C++/WinRT) 时,您将看到 C++/WinRT 特定的签名:

    bool IndexOf(T const& value, uint32_t & index);
    

    你需要更换

    UInt32 bar;
    

    uint32_t bar{};
    

    【讨论】:

    • 可爱,非常明显,但我会让这个问题保持不变,因为它让一些开发人员感到困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 2010-10-19
    • 1970-01-01
    相关资源
    最近更新 更多