Reputation: 79
I was trying to go acording to this tutorial: https://ikriv.com/blog/?p=2470. So You create a WPF app and program.cs with this code:
class Program
{
[STAThread]
public static void Main(string[] args)
{
var app = new App();
app.InitializeComponent();
app.Run();
}
}
Then just change the Startup and Output type.
However, this solution appears to break in the new .NET 5.0 while it works fine in .NET Framework.Is there a way to do this similarly easy in the new version? Thank you very much.
Upvotes: 1
Views: 2796
Reputation: 79
The comment from Robert Harvey solves the question. I used this tutorial (https://ikriv.com/blog/?p=2470) and for a .NET 5.0 I followed this:(https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/5.0/automatically-infer-winexe-output-type)
You can not accept a comment, so I hope it does not matter if I write the summary like this.
Upvotes: 1
Reputation: 1038
@ Naman Kumar.
For opening Console application you could manually open Console window in WPF application( .NET 5.0). You can add the AllocConsole and FreeConsole methods to your class and use it as shown below:
public partial class MainWindow : Window
{
[DllImport("Kernel32")]
public static extern void AllocConsole();
[DllImport("Kernel32", SetLastError = true)]
public static extern void FreeConsole();
public MainWindow()
{
InitializeComponent();
OutputTextBox.Focus();
AllocConsole();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var line = OutputTextBox.Text;
Console.Out.WriteLine(line);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
FreeConsole();
}
}
Upvotes: 2