Develop C#(GUI)-code in Linux (updated: 2016-04-26, to be continued during this week)
In my current position I work a lot in Visual studio and C# to develop software for MS Windows.
When it comes to cross compiling, it is possible to develop (and run) C# (.Net 4.0) applications under Linux environment using monodevelopment IDE.
One question arise when it comes to GUI development. There are two main options.
1) Use Windows Forms (Widely used in c# code in Visual studio)
2) Use GTK# Forms (Needs extra installs of mono and GTK# to run in Window environment)
Most of the people that develop software for windows use Windows forms and not GTK#. To quickly create software for Linux, it would be of great interest to use Windows forms. Is this possible and how?
Is it possible and I will walk you through one way to do this.
1)
Create
a Windows Forms (C#) project in Visual Studio 2015 and copy the
project to Linux. The forms can be empty, but they do need to exist in
the
project before it is copied to Linux. Switch to Linux and start
monodevelopment and open the Visual Studio project. Do also open
wmf-designer and open the form you would like to edit. You can edit the
form and save it and continue to edit the C#-code in monodevelopment.
One big difference is that the subroutines for the event signals
(e.g. button clicked) are not create automatically, and have to be
added.
To add a signal open the designer file: (e.g. form1.designer.cs) and add this line:
this.button1.Click += new System.EventHandler(this.button1_Click);
To add the appropriate function open the .cs file (e.g. form1.cs):
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "Hello World";
}
You can download the simple example files from here (works in both windows and linux).
2) use GTK# forms.
Start a new solution in monodevelop and choose other->Gtk# 2.0
Project. In windows the programfile is called Xamarin studio (the name
of the program is Xamarin also under Linux, but to start the IDE in
Linux you type monodevelop). Choose a name for the project and if you
like to use GIT as VCS, mark use GIT for VCS. Then click finish and an
empty project is created.
Now open: User Interface->MainWindows. Now you think you can add
buttons and so on, but you can´t yet. First you need to add a
container to place your controls in. For this solution I choose a fixed
container. To do this click on toolbox to the right and choose:
Containers->Fixed Now you finally can place your controls almost the
same as in Visual Studio. To set the size of buttons or text fields you
need to manually type a value for HightRequest and WidthRequest
(located in Common Widget Properties. To add a signal and autocreate
function to handle the signal, go to Signals. For buttons choose Button
Signals and dubbelclick on Clicked.
Instead
of using a fixed container you can use Vpaned or Vbox etc and this
is the prefered way according to every manual describing GTK#, But If
you work,as I, normally with Visual Studio, I do think you really
want to use fixed as it behaves more like how to place controls in
Visual Studio.
You can run this in Windows but you need to install GTK libraries to run it.
You find the source code for this simple example here.
GTK#example.zip
Link to download monodev for Linux (openSuse):
monodev
Link to xmf-designer (compiles easiest in Linux, but the exe can be copied and run in Windows):
mwf-designer
Link to Xamarin for windows:
Xamarin
Link to mono and GTK# for Windows
That is all for today.