Search code examples
c#linuxwpfconsole.net-8.0

Is it possible to run a WPF application that is started as an console application in Linux


I have a test WPF application that I have changed so it now can startup as an console application, like this. How to be dynamically either console application or Windows Application

My application is created so that it is not using any WPF GUI packages when run as console, I have done this with a interface so when I set a textbox or write to console it is done like this

  public interface IUserOutput
  {
     abstract void DisplayOutput(string msg);
  }

 class ConsoleOutput : IUserOutput
  {
    public void DisplayOutput(string msg)
    {
       Console.WriteLine(msg);
    }
  } 

 public class TextBoxOutput : IUserOutput
  {
    public void DisplayOutput(string msg)
    {
       if (TestWPF.App.Current != null)
       {  
         TestWPF.App.Current.Dispatcher.BeginInvoke(new Action(() =>
         {
             ((MainWindow)TestWPF.App.Current.MainWindow).TextBox.Text += msg + "\r\n";
         }));
     }
   }
 }

And in the code I either initialize the WPF or Console implementation.

If i try to run it as a console application in Linux I get an error that it is missing "Microsoft.WindowsDesktop.App", version 8.0.0 (x64)

I know it is not possible to run WPF with GUI on Linux, but it is somehow possible to run a WPF application run as a console application in Linux.

I tried to download the nuget package for "Microsoft.WindowsDesktop.App" and put the ref folder inside /usr/dotnet/shared/Microsoft.WindowsDesktop.App/8.0.6/ and /usr/dotnet/shared/Microsoft.WindowsDesktop.App/8.0.0/, but it gives me the same error, it is somehow possible to make Linux think that it is installed, so that it will run it anyways?

Or is there a way to build the project so that Linux can't see that it requires "Microsoft.WindowsDesktop.App"?

Thanks


Solution

  • Assuming you have a very good reason to do so (eg the extraction of relevant parts of software into .NET core+ as a console app is out of question) you can run WPF on Linux, but using Wine. It will also actually emulate the GUI on Linux. Refer to this article : https://ccifra.github.io/PortingWPFAppsToLinux/Overview.html