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>
相关文章
相关标签/搜索