WPF blazor hybrid application with console output window

William Liu 266 Reputation points
2024-04-03T06:57:30.51+00:00

I am following the guid in learn.microsoft.com to develop a .net8 WPF blazor hybrid app. Here is the link.

I build and run the WPF application successfully according to this guid. However, I can`t make the WPF with a Console window even if I set the following attributes to csproj. It will cause a build error.

    <OutputType>Exe</OutputType>
    <DisableWinExeOutputInference>true</DisableWinExeOutputInference>

I have achieved this successfully in my other normal WPF applications. Could you let me know if it is possiable to add the console output window to the WPF/blazor hybrid project in .net8?

Thanks!

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,675 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,394 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 38,331 Reputation points Microsoft Vendor
    2024-04-03T09:41:26.1766667+00:00

    Hi,@William Liu. Welcome to Microsoft Q&A.

    In a .NET 8 WPF Blazor hybrid application, adding a console output window directly to the WPF part of the project might not be straightforward due to the nature of the Blazor hosting model.

    You could try to redirect the standard output stream to a console window in a WPF application and see if that helps.

    In the App class constructor:

    
    using System;
    
    using System.IO;
    
    using System.Runtime.InteropServices;
    
    using System.Windows;
    
    namespace YourNamespace
    
    {
    
        public partial class App : Application
    
        {
    
            [DllImport("kernel32.dll")]
    
            private static extern bool AllocConsole();
    
            protected override void OnStartup(StartupEventArgs e)
    
            {
    
                base.OnStartup(e);
    
                // Allocating a console window
    
                AllocConsole();
    
                // Redirecting standard output stream to the console
    
                Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
    
            }
    
        }
    
    }
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,526 Reputation points
    2024-04-08T20:37:04.71+00:00

    see this thread to capture webview2 console output. You would put the code in the blazor hosting app, not the blazor code:

    https://github.com/MicrosoftEdge/WebView2Feedback/issues/2982

    0 comments No comments