C# dotnet core + GTK# 開發桌面軟件,hello world

以前的話

背景:
GUI 設計可能會遇到的兩個階段:git

  • 界面複雜度,按照對於 既有控件的依賴度,分爲 簡單項目、複雜項目。簡單項目 不涉及 custom control, 只用自帶的控件就能夠知足項目要求;複雜項目,須要本身作 custom control
  • 在 作簡單項目(不涉及 custom control, 只用自帶的控件,例子 1) 的時候,RAD tool 裏 有不少 既有控件,能夠直接快速拖出界面,快速快在這裏
  • 在 custom control 的時候 (作複雜項目)(例子 1 2),必定會用到代碼,這時候 最後有大量的教程參考 反而能提升開發速度,快速快在這裏

評估 GTK#github

  • 缺點:在純代碼堆圖 (在第二階段,設計自定義模塊(代碼畫圖, custom control )) 的時候,教程太少了 (遠不及 System.Windows.Forms 的 自定義模塊 / custom control 的 教程 多)(甚至不及 WPF (XAML based WPF 是什麼 1 2 ) )
  • 優勢:Glade 做爲 RAD tool ,在第一階段,對 既有控件 能夠有快速拖拉出效果的感受

對 使用 GTK# 的警告 ⚠️

若是你須要作複雜界面 / 複雜項目 (參考上面的例子),那麼 在沒有大量教程參考的狀況下,你只能對照 GTK C++ 教程 本身去琢磨對應的 GTK# C# 的實現。
你會哭死。

下一集聊一聊 C# + GTK 開發的另外一種方式: Xamarin.Forms.Platform.GTK 
這裏 Xamarin Forms 是一個 Renderer 
https://github.com/jsuarezruiz/xamarin-forms-gtk-samples
https://github.com/jsuarezruiz/forms-gtk-progress
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/other/gtk 
https://www.nuget.org/packages/Xamarin.Forms.Platform.GTK 

對比,看看你喜歡哪一個風味 的 API :
GTK# 那一套玩意 -> 
http://zetcode.com/gui/gtksharp
主要 GUI 組件包 是 GTK# (一次開發 多處運行)

Xamarin Forms 那一套玩意 -> 
https://github.com/jsuarezruiz/xamarin-forms-gtk-samples 
主要 GUI 組件包 是 Xamarin Forms
涉及到 GTK 的部分僅僅是 「適配」 到 GTK (一次開發 屢次適配 多處運行)

正文

配置步驟:web

  1. 安裝 dotnet core

https://dotnet.microsoft.com/...macos

  1. 安裝 GTK

brew install gtk+3windows

  1. 安裝 GTK# 的 template

dotnet new --install GtkSharp.Template.CSharpapp

  1. 安裝 Glade (RAD工具,快速拖拉控件) 1

brew install glade工具

  1. 新建項目

dotnet new gtkapp -o MyApp1
cd MyApp1
dotnet run佈局

圖片描述

參考:
https://github.com/GtkSharp/G...
dotnet core 可安裝的模板 1
dotnet core 自定義安裝模板 2 2
<TargetFramework>netcoreapp2.1</TargetFramework> 是什麼 1
GTK+3 1
Glade for GTK+3 1post

命令:
查看全部已安裝的 templates
dotnet new --list
依據某個 template 創建項目,好比 console template
dotnet new console -o myApp學習

GTK#
GtkSharpTutorials 1

更好的辦法:
Xamarin Forms 做爲 GTK# 的 wrapper,這樣就不用學習 GTK 那一套玩意了

參考 https://www.reddit.com/r/dotn...

-=-=-

另外一個理由,爲何不用 GTK# ,而用 Xamarin.Forms.Platform.GTK :
一樣是 標記語言佈局技術, xaml 的可讀性 比 glade 好多啦

// 選自 `dotnet new console -o myApp` 生成的 MainWindow.glade
// 這是 GUI 組件 主要生效自的地方
<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk+" version="3.18"/>
  <object class="GtkWindow" id="MainWindow">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Example Window</property>
    <property name="default_width">480</property>
    <property name="default_height">240</property>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="margin_left">4</property>
        <property name="margin_right">4</property>
        <property name="margin_top">4</property>
        <property name="margin_bottom">4</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkLabel" id="_label1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Hello World!</property>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="_button1">
            <property name="label" translatable="yes">Click me!</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
// Xamarin.Forms.Platform.GTK 裏,由 Xamarin Forms 負責 GUI 組件包
// 這是 GUI 組件 主要生效自的地方
// https://github.com/jsuarezruiz/xamarin-forms-gtk-samples/blob/master/BoxView/TextDecoration/TextDecoration/TextDecoration/MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TextDecoration"
             x:Class="TextDecoration.MainPage">
    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness">
            <On Platform="iOS" Value="0, 20, 0, 0" />
        </OnPlatform>
    </ContentPage.Padding>

    <ContentPage.Resources>
        <ResourceDictionary>
            <Style TargetType="BoxView">
                <Setter Property="Color" Value="Black" />
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>

    <ScrollView Margin="15">
        <StackLayout>
            <AbsoluteLayout>
                <BoxView AbsoluteLayout.LayoutBounds="0, 10, 200, 5" />
                <BoxView AbsoluteLayout.LayoutBounds="0, 20, 200, 5" />
                <BoxView AbsoluteLayout.LayoutBounds="10, 0, 5, 65" />
                <BoxView AbsoluteLayout.LayoutBounds="20, 0, 5, 65" />
                <Label Text="Stylish Header"
                       FontSize="24"
                       AbsoluteLayout.LayoutBounds="30, 25, AutoSize, AutoSize"/>
            </AbsoluteLayout>

            <Label>
                <Label.FormattedText>
                    <FormattedString>
                        <Span Text="The " />
                        <Span Text="Label"
                              FontAttributes="Italic" />
                        <Span Text=" and four " />
                        <Span Text="BoxView"
                              FontAttributes="Italic" />
                        <Span Text=" elements shown above are children of an " />
                        <Span Text="AbsoluteLayout"
                              FontAttributes="Italic" />
                        <Span Text=". This allows the text and its decorations to be precisely sized and positioned." />
                    </FormattedString>
                </Label.FormattedText>
            </Label>

            <StackLayout HorizontalOptions="Center">
                <Label Text="Underlined Text"
                       FontSize="24" />
                <BoxView HeightRequest="2" />
            </StackLayout>

            <Label>
                <Label.FormattedText>
                    <FormattedString>
                        <Span Text="The underlined " />
                        <Span Text="Label"
                              FontAttributes="Italic" />
                        <Span Text=" above shares a " />
                        <Span Text="StackLayout"
                              FontAttributes="Italic" />
                        <Span Text=" with a " />
                        <Span Text="BoxView"
                              FontAttributes="Italic" />
                        <Span Text=", whose width is governed by the " />
                        <Span Text="Label"
                              FontAttributes="Italic" />
                        <Span Text=". Unfortunately, you can't use this technique to underline a single word in a paragraph." />
                    </FormattedString>
                </Label.FormattedText>
            </Label>

            <BoxView HeightRequest="3" />

            <Label>
                <Label.FormattedText>
                    <FormattedString>
                        <Span Text="You can also use a " />
                        <Span Text="BoxView"
                                  FontAttributes="Italic" />
                        <Span Text=" for a horizontal line. Specify a height but let the width fill the horizontal dimensions of the container." />
                    </FormattedString>
                </Label.FormattedText>
            </Label>

            <StackLayout Orientation="Horizontal">
                <BoxView WidthRequest="4"
                         Margin="0, 0, 10, 0" />
                <Label>
                    <Label.FormattedText>
                        <FormattedString>
                            <Span Text="Similarly, you can use a " />
                            <Span Text="BoxView"
                                  FontAttributes="Italic" />
                            <Span Text=" to mark off a paragraph of text, for example, to indicate a quotation. In this case, the " />
                            <Span Text="Label"
                                  FontAttributes="Italic" />
                            <Span Text=" and " />
                            <Span Text="BoxView"
                                  FontAttributes="Italic" />
                            <Span Text=" also share a " />
                            <Span Text="StackLayout"
                                  FontAttributes="Italic" />
                            <Span Text=", but with a horizontal orientation." />
                        </FormattedString>
                    </Label.FormattedText>
                </Label>
            </StackLayout>

            <Label>
                <Label.FormattedText>
                    <FormattedString>
                        <Span Text="What can you use " />
                        <Span Text="BoxView"
                                FontAttributes="Italic" />
                        <Span Text=" for?" />
                    </FormattedString>
                </Label.FormattedText>
            </Label>
        </StackLayout>
    </ScrollView>
</ContentPage>
相關文章
相關標籤/搜索