将变量值打印到控制台visual c ++ windows存储Modern UI app

时间:2013-06-20 16:59:53

标签: windows windows-8 windows-store-apps visual-studio-debugging c++-cx

我刚刚开始学习C ++ / XAML Windows应用程序开发,但对于我的生活,我找不到一个很好的方法来将变量值打印到"输出" VS2012中的窗口。

Debug.WriteLine()似乎不适用于Windows应用商店应用,但我无法找到除OutputDebugString()之外的其他打印方式,而我无法使用它来打印变量值(没有一些繁重的格式化)。

是否只有一种简单的方法来打印示例行:

  

小鼠位置X:12

例如,

,其中12是来自MouseDelta的整数。

感谢您的时间,

雨披

3 个答案:

答案 0 :(得分:1)

这是一个不错的选择:http://seaplusplus.com/2012/06/25/printf-debugging-in-metro-style-apps/,基本上它为您的Windows应用商店App分配一个控制台,显然这将失败认证,但鉴于这可能只是出于调试目的,它会没问题。我在这里复制相关代码:

// Include Windows.h for WINBASEAPI and WINAPI:
#include <Windows.h>

// Declare AllocConsole ourselves, since Windows.h omits it:
extern "C" WINBASEAPI int WINAPI AllocConsole();

auto main(Platform::Array<Platform::String^>^) -> int
{
    AllocConsole();

    std::wcout << L"Hello there!" << std::endl;
    std::getchar();

    return EXIT_SUCCESS;
}

但是,如果您希望在应用程序中看到此类输出,则可能需要使用Console Class for Modern UI Apps实现.NET System.Console的一部分,并且可以在Windows应用商店应用中安全使用。

答案 1 :(得分:1)

This solution使用OutputDebugString附近的包装:

void WinLog(const wchar_t *text, int n)
{
    wchar_t buf[1024];
    _snwprintf_s(buf, 1024, _TRUNCATE, L"%s %d\n", text, n);
    OutputDebugString(buf);
}

可以如下调用:

WinLog(L"The Answer is", 42);

答案 2 :(得分:0)

不是,不。您可以编写一个格式为printf的小函数,并将生成的字符串传递给OutputDebugString(),但没有更简单的可用。

我猜您可以使用ToString()Platform::String::operator+Platform::String::Data()来完成此任务;虽然它有点难看:

OutputDebugString( ("mouse position X:" + MouseDelta.X.ToString())->Data() );