Although WinForms may be “dead”, it does have one trick up its sleeve that WPF doesn’t, and that is you can run WinForms apps on mono. Here’s a simple guide to running a Windows Forms application on Ubuntu
Step 1 - Install Mono
Open a terminal window, and make sure everything is up to date with the following commands:
sudo apt-get update sudo apt-get upgrade
Now you can install mono with the following command:
sudo apt-get install mono-complete
Step 2 - Create an Application
Now we need to create our C# source file. You can use any text editor you like, but if like me you aren’t familiar with Linux text editors like vi or emacs, gedit is a simple notepad-like application which is easy to use. Launch it with the following command: (the ampersand at the end tells the terminal not to wait for gedit to close before letting us continue)
gedit wf.cs &
Now let’s create a very simple application:
using System; using System.Windows.Forms; public class Program { [STAThread] public static void Main() { var f = new Form(); f.Text = "Hello World"; Application.Run(f); } }
Step 3 - Compile and Run
Now we’re ready to compile. The C# compiler in mono is gmcs. We’ll need to tell it we’re referencing the Windows Forms DLL:
gmcs wf.cs –r:System.Windows.Forms.dll
To run the application, simply call mono, passing in the executable:
mono wf.exe
And that’s all there is to it! We have a WinForms app running on Linux.
Although mono doesn’t support everything in WinForms, you can use most standard controls, so you can easily add further UI elements:
Taking it Further
Obviously writing applications by hand like this is a bit cumbersome, but there is an IDE you can use for Linux called monodevelop. You install it like this:
sudo-apt-get install monodevelop
This then gives you a nice editing environment, allowing you to debug, and manage project references (you’d usually add System.Windows.Forms and System.Drawing). Unfortunately it doesn’t offer a WinForms designer – for desktop apps it prefers you to use GTK#. Nevertheless, it’s a nice free IDE allowing you to experiment with getting your existing Windows Forms applications working cross-platform on Linux. (It seems this will also work on OS X with mono installed but I don’t have a Mac so I haven’t tried it out)
No comments:
Post a Comment