mono跨平臺GUI庫-Eto

潛水很久,也不知道寫些什麼,就放點好玩的東西,目前.net雖然開源了,但對跨平臺這塊,還得依靠mono,之前在windows下作界面編程,用到的就是winform和wpf,mono雖然支持winform,可是在Linux下的表現就好像那時候的windows95通常。因此要麼就用GTK#,GTK#是GTK+的綁定,GTK的那些文檔看得頭疼。因此就找到這個Etogit

兩年前寫過一個小工具,串口助手,很簡單的,就拿它來作個演示,下面是先前的一個界面。github

1.新建控制檯項目編程

2.將項目屬性輸出類型改成Windows應用程序windows

3.NuGet添加工具

Install-Package Eto.Forms佈局

Install-Package Eto.Platform.Gtkthis

Install-Package Eto.Platform.Windowsspa

4.建立MainForm.net

1 public partial class MainForm : Form
2    {
3        public MainForm()
4        {
5             InitializeComponent();
6        }
7    }            

建立UI3d

 partial class MainForm
    {
        private void InitializeComponent()
        {
            var layout_left = new DynamicLayout { Padding = new Padding(10, 5, 5, 5), Spacing = new Size(5, 5), ClientSize = new Size(345, 426), MinimumSize = new Size(345, 426) };
            var layout_right = new DynamicLayout { Padding = new Padding(5, 5, 5, 5), Spacing = new Size(5, 5), ClientSize = new Size(180, 426), MinimumSize = new Size(180, 426) };

            var layout_serialPort = new DynamicLayout { Padding = new Padding(5, 15, 5, 5), Spacing = new Size(5, 5) };


            _textAreaIn = new TextArea() { BackgroundColor = Colors.Black, TextColor = Colors.SpringGreen, };
            _textAreaOut = new TextArea() { BackgroundColor = Colors.Black, TextColor = Colors.SpringGreen, };
            layout_left.AddAutoSized(new Label() { Text = "接收區" });
            layout_left.AddSeparateRow(new Panel() { Content = _textAreaIn, MinimumSize = new Size(345, 240), ClientSize = new Size(345, 240) });
            layout_left.AddAutoSized(new Label() { Text = "發送區" });
            layout_left.AddSeparateRow(new Panel() { Content = _textAreaOut, MinimumSize = new Size(345, 165), ClientSize = new Size(345, 165) });

            _comboBoxSerialPortName = new ComboBox();
            _comboBoxSerialBaudRate = new ComboBox();
            _comboBoxSerialDataBits = new ComboBox();
            _comboBoxSerialParity = new ComboBox();
            _comboBoxSerialStopBits = new ComboBox();

            _btnClear = new Button { Text = "清空收區" };
            _btnClear.Click += _btnClear_Click;
            _btnOpen = new Button { Text = "打開串口" };
            _btnOpen.Click += _btnOpen_Click;
            _btnSend = new Button { Text = "串口發送" };
            _btnSend.Click += _btnSend_Click;
            _checkBoxHex = new CheckBox{ Text = "是否Hex"};
            label7 = new Label();

            layout_serialPort.BeginVertical();
            layout_serialPort.BeginHorizontal();
            layout_serialPort.AddAutoSized(new Label() { Text = "端口號:" }, new Padding(0, 3));
            layout_serialPort.AddAutoSized(_comboBoxSerialPortName);
            layout_serialPort.EndBeginHorizontal();
            layout_serialPort.BeginHorizontal();
            layout_serialPort.AddAutoSized(new Label() { Text = "波特率:" }, new Padding(0, 3));
            layout_serialPort.AddAutoSized(_comboBoxSerialBaudRate);
            layout_serialPort.EndBeginHorizontal();
            layout_serialPort.BeginHorizontal();
            layout_serialPort.AddAutoSized(new Label() { Text = "數據位:" }, new Padding(0, 3));
            layout_serialPort.AddAutoSized(_comboBoxSerialDataBits);
            layout_serialPort.EndBeginHorizontal();
            layout_serialPort.BeginHorizontal();
            layout_serialPort.AddAutoSized(new Label() { Text = "效驗位:" }, new Padding(0, 3));
            layout_serialPort.AddAutoSized(_comboBoxSerialParity);
            layout_serialPort.EndBeginHorizontal();
            layout_serialPort.BeginHorizontal();
            layout_serialPort.AddAutoSized(new Label() { Text = "中止位:" }, new Padding(0, 3));
            layout_serialPort.AddAutoSized(_comboBoxSerialStopBits);
            layout_serialPort.EndBeginHorizontal();
            //layout_serialPort.AddColumn();
            //layout_serialPort.AddColumn();

            layout_serialPort.EndBeginVertical();

            layout_right.AddAutoSized(layout_serialPort);
            layout_right.AddCentered(new Panel() { Content = _btnClear, ClientSize = new Size(164, 37), MinimumSize = new Size(164, 37) }, null, null, true);
            layout_right.AddCentered(new Panel() { Content = _btnOpen, ClientSize = new Size(164, 37), MinimumSize = new Size(164, 37) }, new Padding(0, 80, 0, 0), null, true);
            layout_right.AddCentered(new Panel() { Content = _btnSend, ClientSize = new Size(164, 37), MinimumSize = new Size(164, 37) }, new Padding(0, 10), null, true);
            layout_right.AddCentered(_checkBoxHex);
            layout_right.AddAutoSized(label7);

            var layout = new Splitter()
            {
                Panel1 = layout_left,
                Panel2 = layout_right,
                Orientation = SplitterOrientation.Horizontal
            };
            this.Content = layout;
            this.Title = "串口助手";
        }

       

      

        private TextArea _textAreaIn;
        private TextArea _textAreaOut;

        private ComboBox _comboBoxSerialPortName;
        private ComboBox _comboBoxSerialBaudRate;
        private ComboBox _comboBoxSerialDataBits;
        private ComboBox _comboBoxSerialParity;
        private ComboBox _comboBoxSerialStopBits;

        private Button _btnClear;
        private Button _btnOpen;
        private Button _btnSend;
        private CheckBox _checkBoxHex;
        private Label label7;
    }
class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            new Application().Run(new MainForm());
        }
    }

Eto有如下佈局類:

  • Panel - Controls that subclass Panel, such as WindowGroupBoxScrollable, etc allow you to specify a single child Content control
  • TableLayout - Similar to how an HTML table works, with a single control per cell
  • PixelLayout - Specify X,Y co-ordinates for the position of each control (from Upper-Left)
  • DynamicLayout - Dynamic hierarchical horizontal and vertical layout

至關於控件容器,用於Control的擺放,下面就看看各個平臺下的顯示效果 ^_^

1.windows下:

2Ubuntu下:

3.最近有人在的玩樹莓派:

下載地址:代碼

比較簡單,只是想說,咱dotNet跨平臺仍是很好玩的哈~純粹拋磚引玉哈。。。

相關文章
相關標籤/搜索