Linux图形界面开发—monodevelop初探

在ubutu10.04下,如果通过源码安装monodevelop有问题,建议用ubuntu自带的软件包管理器安装。

下面通过几个例子测试下monodevelop

(1)控制台应用程序

u

强大的界面于windows下的vs差不多。输入项目的名称,保存位置,解决方案的名称,保存位置,与windows下一样的哦,其他设置都默认。

一切都是那么的熟悉,c#代码。编译好后,会生成一个consol.exe的可执行文件,直接F5运行可以得到输出结果

也可以通过命令guoyuanwei@localhost:~$ mono /home/guoyuanwei/monotest/consol/consol/bin/Debug/consol.exe 得到结果
Hello World!
(2)Gtk#图形界面的程序

在上面创建的解决方案上,点击鼠标右键-》Add-》Add New Project添加一个新的项目,在弹出的窗口中选择GTK#2.0,最后生成的界面如下

可以看到左边解决方案的下面多了一个项目GtkTest,编译此项目,运行后得到如下界面

上面的界面有点简单,下面加以改进,点击查看-》属性和工具栏,即打开工具栏和熟悉窗口,下面是一部份工具栏窗口,可以适当的修改窗体界面,通过工具栏。

修改后界面如下:

代码如下:

using System;
using Gtk;

public partial class MainWindow : Gtk.Window
{
	public MainWindow () : base(Gtk.WindowType.Toplevel)
	{
		Build ();
	}

	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}
	
	//打开日志进行查看
	protected virtual void OnOpen (object sender, System.EventArgs e)
	{
		 // Reset the logTreeView and change the window back to original size
     int width, height;
     this.GetDefaultSize( out width, out height );
     this.Resize( width, height );
     
     LogView.Buffer.Text = "";
     
     // Create and display a fileChooserDialog
     FileChooserDialog chooser = new FileChooserDialog(
        "Please select a logfile to view ...",
        this,
        FileChooserAction.Open,
        "Cancel", ResponseType.Cancel,
        "Open", ResponseType.Accept );
     
     if( chooser.Run() == ( int )ResponseType.Accept )
     {
        // Open the file for reading.
        System.IO.StreamReader file =
        System.IO.File.OpenText( chooser.Filename );
        
        // Copy the contents into the logTextView
        LogView.Buffer.Text = file.ReadToEnd();
        
        // Set the MainWindow Title to the filename.
        this.Title = "Nate's Log Viewer -- " + chooser.Filename.ToString();
        
        // Make the MainWindow bigger to accomodate the text in the logTextView
        this.Resize( 640, 480 );
        
        // Close the file so as to not leave a mess.
        file.Close();
     } // end if
     chooser.Destroy();
	}
	//关闭应用程序
	protected virtual void OnCloseActivated (object sender, System.EventArgs e)
	{
		Application.Quit();
	}
如果要开发出更加复杂的图形界面在linux上,还得继续研究GTK#,不过总体思路和winform开发一致。



posted @ 2012-03-18 18:03  郭远威  阅读(6087)  评论(0编辑  收藏  举报