Since its inception .NET has captivated developers across the world with its simplicity of development and its flexibility in deploying enterprise-class applications. Let’s face it; today, Windows applications are reaching new frontiers in the software development spheres of the desktop, the Web and rich UX applications.
And these types of applications have found a new home, the Linux platform. Often, many developers have found deploying .NET apps to Linux challenging. In this short how-to, we will learn how to develop a .NET 4.0 RichTextPad, and deploy it to a Mono 2.8 Linux environment, without changing a single line of code.
Embarcadero Delphi Prism XE provides a cross-platform development solution and a robust programming language for rapidly developing .NET, Mono, ASP.NET and data-driven applications for Windows, Linux and Mac OS X. Delphi Prism combines an easy-to-learn syntax, based on the Delphi language, with features not available in other .NET programming languages — enabling developers to create cutting-edge applications. With Delphi Prism, you can:
- Create .NET, ASP.NET Web, and Silverlight applications.
- Build cross-platform Mono applications and deploy them on Windows, Linux and Mac OS X.
- Develop apps with a choice of IDEs, on both Windows and Mac OS X.
- Leverage your Delphi language skills with the easy-to-learn Delphi Prism language.
- Work with the latest .NET 4.0 and Visual Studio 2010 technologies.
Requirements
- You need to do a Web registration for an Embarcadero Developer Network account, which is free. Visit the member area to sign up. Next, download a trial copy of Delphi Prism. Click the first link, which says “Delphi Prism XE Enterprise – 30 day trial”.
- The openSUSE Mono 2.8 Virtual Appliance.
- VMWare/VirtualBox for hosting the Virtual Appliance.
Setup
- Install Delphi Prism XE just like any other Windows application.
- Set up the VM — launch VMware Player/VirtualBox, and create a new openSUSE-based VM; boot off the Live CD ISO (
Mono-2.8.1.iso
) and then choose Install. The recommended configuration is to have swap space of twice your physical memory; the remaining space can be allocated to the/
(root) partition. - Once installed, reboot the VM and log in with your credentials.
- Ensure that VMWare Tools/VirtualBox Guest Additions are installed in the guest OS.
Building a simple notepad application
- Launch Delphi Prism by clicking Start –> All Programs –> Delphi Prism in Visual Studio 2010. Ensure that you run Visual Studio as an administrator.
- Select File –> New Project and choose Windows Forms Application (Mono), under the Mono section, as shown in Figure 1.
- From the toolbox, drag and drop the following controls onto the form:
- MenuStrip
- ToolStrip
- StatusStrip
- StatusStrip
- ContextMenuStrip
- RichTextBox control
- For the MenuStrip and ToolStrip controls, set to insert standard items.
- Dock the RichTextBox control.
- For the ContextStrip menu, add Cut, Copy and Paste items. The final UI should resemble Figure 2.
- Add some code to implement simple Cut, Copy and Paste functionality:
[Cut Action] method MainForm.cutToolStripMenuItem1_Click(sender: System.Object; e: System.EventArgs); begin richTextBox1.Cut; end; [Copy Action] method MainForm.copyToolStripMenuItem1_Click(sender: System.Object; e: System.EventArgs); begin richTextBox1.Copy; end; [Paste Action] method MainForm.pasteToolStripMenuItem1_Click(sender: System.Object; e: System.EventArgs); begin richTextBox1.Paste; end; end.
- Bind the ContextMenuStrip to the RichTextBox, and copy and paste the above code for the respective actions, in the ContextMenuStrip.
- Press F5 and run the application. Type some text and right-click to show the context menu, as shown in Figure 3. Verify that the added functions work, and close the application.
Deploying the application to Linux (the Mono appliance)
- Make sure you have the Mono appliance running.
- Navigate to the Debug folder of your project. The path will be similar to
C:\Users\Username\Documents\visual studio 2010\Projects\Simple Notepad\Simple Notepad\bin\Debug
. - Copy the
Simple_Notepad.exe
to the Mono appliance desktop. Now on your Mono desktop, you should see an icon, as shown in Figure 4. - Before we proceed, we should ensure that the application does not have any dependencies that prevent it from running successfully in our Mono environment. To test this, click Computer, and choose MoMA (Mono Migration Analyzer), as shown in Figure 5. Click Next to continue.
- Now click the (+) button and add
Simple_Notepad.exe
to the Analyzer, as shown in Figure 6. - Click Next to continue. It will start scanning the assemblies needed for a successful launch. You can see whether the MoMA tool has found any errors, as shown in Figure 7. It clearly indicates that one can run the .NET app in the Mono environment without any difficulties. Click Next twice to continue, and click Close.
- Now, launch a terminal window, and run
mono Simple_Notepad.exe
. You should see the application running without any problems, as in the composite Figure 8, which shows the application running on both Windows and Linux.
Congratulations! You have successfully ported a .NET application to Linux, using Delphi Prism, without having to change a single line of code.