AvaloniaUI - 跨平台.NET UI框架

AvaloniaUI - 跨平台.NET UI框架

项目描述

Avalonia是一个基于.NET的跨平台UI框架,被认为是WPF的精神继承者。它支持以下平台:

  • Windows
  • macOS
  • Linux
  • iOS
  • Android
  • WebAssembly

项目采用MIT许可证,已被多家知名企业采用,包括Schneider Electric、Unity、JetBrains和GitHub等。

功能特性

  • 跨平台支持:一套代码可运行在多个平台上
  • 灵活的样式系统:类似CSS的样式机制
  • 丰富的控件库:包含常用UI控件和布局容器
  • 高性能渲染:支持硬件加速
  • XAML支持:使用XAML进行界面设计
  • 数据绑定:强大的数据绑定功能
  • 可扩展架构:支持自定义控件和渲染器
  • 原生互操作:提供与原生平台交互的能力

安装指南

系统要求

  • .NET SDK 6.0或更高版本

安装步骤

  1. 安装.NET SDK
  2. 创建新项目:
    dotnet new install Avalonia.Templates
    dotnet new avalonia.app -o MyApp
    cd MyApp
    
  3. 运行项目:
    dotnet run
    

使用说明

基本示例

创建一个简单的Avalonia应用程序:

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace MyApp
{
    public class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }
    }
}

数据绑定示例

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="MyApp.MainWindow">
    <StackPanel>
        <TextBox Text="{Binding Name}"/>
        <TextBlock Text="{Binding Greeting}"/>
    </StackPanel>
</Window>

样式示例

<Style Selector="Button">
    <Setter Property="Background" Value="Red"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Padding" Value="10"/>
</Style>

核心代码

窗口实现 (WindowImpl.h)

class WindowImpl : public virtual WindowBaseImpl, 
                   public virtual IAvnWindow, 
                   public IWindowStateChanged
{
public:
    FORWARD_IUNKNOWN()
    
    WindowImpl(IAvnWindowEvents* events);

    virtual HRESULT Show(bool activate, bool isDialog) override;
    virtual HRESULT SetEnabled(bool enable) override;
    virtual HRESULT SetCanResize(bool value) override;
    virtual HRESULT SetDecorations(SystemDecorations value) override;
    virtual HRESULT SetTitle(char* utf8title) override;
    
    // 窗口状态管理
    void StartStateTransition() override;
    void EndStateTransition() override;
    SystemDecorations Decorations() override;
    AvnWindowState WindowState() override;
    void WindowStateChanged() override;
};

文本输入方法 (AvnTextInputMethod.h)

class AvnTextInputMethod: public virtual ComObject, 
                          public virtual IAvnTextInputMethod
{
private:
    id<AvnTextInputMethodDelegate> _inputMethodDelegate;
    
public:
    AvnTextInputMethod(id<AvnTextInputMethodDelegate> inputMethodDelegate);
    
    virtual HRESULT SetClient(IAvnTextInputMethodClient* client) override;
    virtual void Reset() override;
    virtual void SetCursorRect(AvnRect rect) override;
    virtual void SetSurroundingText(char* text, int anchorOffset, int cursorOffset) override;
    
    ComPtr<IAvnTextInputMethodClient> Client;
};

自定义绘图控件 (CustomDrawingExampleControl.cs)

public class CustomDrawingExampleControl : Control
{
    public static readonly StyledProperty<double> ScaleProperty = 
        AvaloniaProperty.Register<CustomDrawingExampleControl, double>(nameof(Scale), 1.0d);
        
    public double Scale 
    { 
        get => GetValue(ScaleProperty); 
        set => SetValue(ScaleProperty, value); 
    }

    public static readonly StyledProperty<double> RotationProperty = 
        AvaloniaProperty.Register<CustomDrawingExampleControl, double>(
            nameof(Rotation),
            coerce: (_, val) => val % (Math.PI * 2));

    protected override void OnRender(DrawingContext context)
    {
        base.OnRender(context);
        
        // 自定义绘图逻辑
        var rect = new Rect(0, 0, Bounds.Width, Bounds.Height);
        context.DrawRectangle(Brushes.Blue, new Pen(Brushes.Red, 2), rect);
    }
}

更多精彩内容 请关注我的个人公众号 公众号(办公AI智能小助手)
公众号二维码

posted @ 2025-08-05 06:01  qife  阅读(13)  评论(0)    收藏  举报