如何在 Windows 通用 C++ 项目中打印

How to print in a Windows Universal C++ project

我觉得问这个问题很蠢,但老实说我不明白为什么不能使用 System 命名空间!我究竟做错了什么?还有其他方法可以在输出中打印一行吗? (我使用的是 Visual Studio 2015)

I can't understand why System namespace can't be used

Windows 通用应用程序与传统桌面应用程序完全不同,请查看 Windows Runtime APIs and Win32 and COM API 其中列出了支持在 UWP 应用程序中使用的所有 Win32 和 COM API。

Is there any other way to print a single line in the output? (I am using Visual Studio 2015)

如果您需要将消息打印到输出 window,请在 UWP C++/CX 项目中使用 OutputDebugString function,添加 #include 以访问它,例如:

void CPPUWPApp1::MainPage::LogMessage(Object^ parameter)
{
    auto paraString = parameter->ToString();
    auto formattedText = std::wstring(paraString->Data()).append(L"\r\n");
    OutputDebugString(formattedText.c_str());
}

用法:

LogMessage("Hello World!");

您可以直接这样做:

OutputDebugString(L"Hello World");

注意字符串前面的L,直接转换为LPCWSTR。