【C#】跨平台UI库Avalonia的学习笔记(上)

目的

因为有这种需求,希望开发一个上位机软件,可以在Windows和Linux上同时运行,而且这个Linux很可能是arm平台的板子,其实就是可以代替一部分工控机的功能,省钱也节约设备占用的体积,鉴于自己现在不太清楚C++,也懒得去折腾QT(毕竟商用要钱,公司并没有整这个,但是公司买了VS),找了找可以实现上位机界面软件的方法,可能这个跨平台UI库Avalonia就比较合适了,对C#已经略知一二的我,能够写Winform小工具,WPF也能了解些了,感觉可以来学一学这个东西了。

这是一个目录

  • 目的
  • Avalonia下,我目前整出的一些实现备忘
    • 窗口标题栏消除掉
    • visualbrush画分割线
    • 样式
    • Command
  • 怎么学
    • 开始
      • Getting Started
      • Programming with Avalonia
      • The Model-View-ViewModel Pattern (MVVM)
      • Controls & Layouts
      • Data Binding
      • Graphics & Animations
      • Windows
      • UserControls
      • Assets
      • Developer Tools
    • Logging Errors and Warnings
    • Unhandled Exceptions
    • Application Lifetimes
      • IControlledApplicationLifetime
      • IClassicDesktopStyleApplicationLifetime
      • ISingleViewApplicationLifetime
    • 绑定
      • DataContext
      • Change Notifications
      • Bindings
      • 一个官方的例子
      • StringFormat
      • Converting Binding Values
      • Binding to Commands
      • 一个官方的例子
      • Binding to Tasks and Observables
      • Binding from Code
        • Subscribing to Changes to a Property
        • Binding to an observable
        • Setting a binding in an object initializer
        • Transforming binding values
        • Using XAML bindings from code
        • Subscribing to a property on any object
        • Binding to INotifyPropertyChanged objects
      • Binding in a Control Template
      • Binding Classes
      • Creating and binding Attached Properties
    • Style
      • Style Classes
      • Pseudoclasses
      • Custom Pseudoclasses
      • Selector
      • Setter
      • Style Precedence
      • Selectors
        • OfType
        • Name
        • Class
        • is
        • Chlid
        • Descendant
        • PropertyEquals
        • Template
        • Not
        • Or
        • Nth Child
        • Nth Last Child
    • Resources
      • Declaring resources
      • Referencing resources
      • Overriding resources
      • Merged resource dictionaries
      • Resource resolution
      • Theme resources
      • Troubleshooting
        • 选中的控件不存在
        • 目标的属性被其他的样式覆写
        • 本地设置的属性覆盖样式
        • 缺少样式伪类选择器
        • 带有伪类的选择器没有将默认的样式覆写
    • 各种各样的控件
    • 模板Template
      • Data Templates
      • 官方的例子
      • 通过代码创建数据模板


Avalonia下,我目前整出的一些实现备忘

窗口标题栏消除掉

        ExtendClientAreaToDecorationsHint="True"
        ExtendClientAreaChromeHints="NoChrome"
        ExtendClientAreaTitleBarHeightHint="-1"

加在window标签下

visualbrush画分割线

这个和WPF下,有点点不一样

DestinationRect=“10,10,10,10” SourceRect=“0,0,0,0”
我感觉是矩形坐标,我画的分割线宽度是10

            <Border.Background>
                <VisualBrush
                    AlignmentX="Left"
                    AlignmentY="Top"
                    DestinationRect="10,10,10,10"
                    SourceRect="0,0,0,0"
                    Stretch="None"
                    TileMode="Tile">
                    <VisualBrush.Visual>
                        <Grid Width="10" Height="10">
                            <Line
                                Stroke="DarkGray"
                                StrokeThickness="2"
                                StartPoint="0,10"
                                EndPoint="10,0" />
                            <Line
                                Stroke="DarkGray"
                                StrokeThickness="2"
                                StartPoint="10,10"
                                EndPoint="0,0" />
                        Grid>
                    VisualBrush.Visual>
                VisualBrush>
            Border.Background>

样式

这里和WPF非常不一样,建议再看看Document
首先新建一个样式文件
我是希望做一个RadioButton,带图标和字,按下对应的背景和字的颜色会变的样式
据它官网说,它这套样式系统类似与CSS,不是WPF那种用样式名字来绑定的,有点不习惯,具体的我还没有学到
看这个似乎它还整出了一个样式预览的功能,这个WPF倒是没有

<Styles xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        "100">