Search code examples
debuggingconsolewinui-3

Why cannot use "console.WriteLine" in winUI3 app (C++)? error: 'Console': is not a class or namespace


I recently write a simple test app(which is a winUI3 app (C++)), and I want to put some message to console, in order to help me with debugging. I used "console.WriteLine" but get this error: 'Console': is not a class or namespace.
I looked up in the Microsoft website, but it couldn't work either.(I could not include "system" in my .cpp file)

the code is following:

using namespace system; // would get this Error:'system': a namespace with this name does not exist

using namespace winrt::Windows::UI::Popups;
using namespace std;
using namespace winrt;
using namespace Windows::Foundation;

using namespace Microsoft::UI::Xaml;
namespace winrt::SampleApp::implementation


{ 
  LoginPage::LoginPage()

{
   InitializeComponent();
   Console::WriteLine("hello!");
}

I have looked up following links, but got no luck. this is for c#

I just want a way to show the messages to help me debug.

Console is a good way, and I also tried OutputDebugString but no message output in the output window. Where did I do wrongly?

Can anyone help me? Thanks a lot! Any suggestion is appreciated!


Solution

  • Why cannot use "console.WriteLine" in winUI3 app (C++)?

    Because System.Console is a .NET API.

    If you want to print something to the debug output window in a WinUI 3 C++ app you could use the OutputDebugStringA function:

    LoginPage::LoginPage()
    {
        InitializeComponent();
        OutputDebugStringA("hello1!\n");
    }