やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

Raspbian stretch MonoDevelop で Gtk# 等UIのラッパ Eto.Forms を使ってみる

 NuGetからダウンロードする。

対象環境

Eto.Forms

 GUIライブラリの違いを吸収するラッパ。クロスプラットフォームで動作するコードを書ける。

手順

1. プロジェクト作成

  1. MonoDevelopを起動する
  2. メニュー→ファイル新しいソリューション
    f:id:ytyaru:20181201194454p:plain
  3. 空のプロジェクト
    f:id:ytyaru:20181201194501p:plain
  4. プロジェクト名場所を入力する
    f:id:ytyaru:20181201194509p:plain
  5. プロジェクトをクリックする
    f:id:ytyaru:20181201194707p:plain

2. Eto.Formsをプロジェクトに追加する

Eto.FormsEto Sampleの両方を入れると起動できなくなるかもしれない。そのときはEto Sampleのみ入れること

  1. メニュー→プロジェクトNuGet パッケージの追加
    f:id:ytyaru:20181201194958p:plain
  2. パッケージを追加ダイアログが表示される
    f:id:ytyaru:20181201195154p:plain
  3. 検索窓にEtoと入力しEto.Formsを探す
    f:id:ytyaru:20181201195312p:plain
  4. パッケージを追加をクリックする
  5. ライセンスの同意同意をクリックする
    f:id:ytyaru:20181201195414p:plain
  6. Eto.Formsが追加された
    f:id:ytyaru:20181201195448p:plain

3. Eto Sampleをプロジェクトに追加する

  1. メニュー→プロジェクトNuGet パッケージの追加
    f:id:ytyaru:20181201194958p:plain
  2. パッケージを追加ダイアログが表示される
    f:id:ytyaru:20181201195154p:plain
  3. 検索窓にEto sampleと入力しEto.Forms.Sampleを探す
    f:id:ytyaru:20181201195728p:plain
  4. Eto.Formsが追加された
    f:id:ytyaru:20181201195751p:plain
    f:id:ytyaru:20181201195820p:plain

4. 実行

  1. Ctrl+F5で実行する
  2. 実行結果ウインドウが出る
    f:id:ytyaru:20181201195846p:plain
  3. Click meをクリックするとダイアログが表示される
    f:id:ytyaru:20181201195939p:plain
  4. メニューが表示される
    f:id:ytyaru:20181201200003p:plain

ソースコード

Program.cs

using System;
using Eto.Forms;
using Eto.Drawing;

namespace HelloEto0
{
    public static class Program
    {
        [STAThread]
        public static void Main(string[] args)
        {
            new Application().Run(new MainForm());
        }
    }
}

MainForm.cs

using System;
using Eto.Forms;
using Eto.Drawing;

namespace HelloEto
{
    public class MainForm : Form
    {
        public MainForm()
        {
            Title = "My Eto Form";
            ClientSize = new Size(400, 350);

            Content = new TableLayout
            {
                Padding = 10,
                Rows = {
                    null,
                    // row with three columns
                    new TableRow(null, new Label { Text = "Hello World!" }, null),
                    null
                }
            };

            // create a few commands that can be used for the menu and toolbar
            var clickMe = new Command { MenuText = "Click Me!", ToolBarText = "Click Me!" };
            clickMe.Executed += (sender, e) => MessageBox.Show(this, "I was clicked!");

            var quitCommand = new Command { MenuText = "Quit", Shortcut = Application.Instance.CommonModifier | Keys.Q };
            quitCommand.Executed += (sender, e) => Application.Instance.Quit();
            
            var aboutCommand = new Command { MenuText = "About..." };
            aboutCommand.Executed += (sender, e) => MessageBox.Show(this, "About my app...");
                    
            // create menu
            Menu = new MenuBar
            {
                Items = {
                    // File submenu
                    new ButtonMenuItem { Text = "&File", Items = { clickMe } },
                    // new ButtonMenuItem { Text = "&Edit", Items = { /* commands/items */ } },
                    // new ButtonMenuItem { Text = "&View", Items = { /* commands/items */ } },
                },
                ApplicationItems = {
                    // application (OS X) or file menu (others)
                    new ButtonMenuItem { Text = "&Preferences..." },
                },
                QuitItem = quitCommand,
                AboutItem = aboutCommand
            };
            
            // create toolbar           
            ToolBar = new ToolBar { Items = { clickMe } };
        }
    }
}