Avalonia的初步学习和使用

本文介绍了如何使用命令行和VisualStudio创建Avalonia应用程序,包括下载模板、新建项目并运行。在VisualStudio中安装Avalonia扩展,并展示了.axaml文件的结构,强调了MVVM模式和数据绑定的概念。此外,文章还概述了Avalonia的组件如控件、输入系统、布局和数据绑定机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、使用Avalonia生成一个窗口

 

步骤1:下载Avalonia的模板

dotnet new install Avalonia.Templates

步骤二:创建一个新的名为MyApp的应用程序

dotnet new avalonia.app -o MyApp

步骤三:导航到MyApp并运行

cd MyApp
dotnet run

2、在Visual Studio中安装Avalonia的扩展

工具:Visual Studio2022

.net版本:.net7.0.201

步骤一:安装avalonia的扩展

点击vs2022中的扩展->搜索avalonia->下载->关闭Visual Studio会自动安装

步骤二:新建一个项目

 

运行截图

.axaml文件说明

  • 要使用控件,必须引入命名空间:xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  • 本案例采用mvvm模式,其中视图是围绕视图模型构建的,所以要在视图中引入视图模型:x:DataType="vm:MainWindowViewModel
  •  其中.axaml控制外观、.cs控制行为
  • 图标:Icon="/Assets/avalonia-logo.ico"
  • 标题:Title="AvaloniaApplication1"
  • 将TextBlock的Text属性绑定到MainWindowViewModel的Greeting方法上:<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
  • 要设置实际的DataContext运行时,在代码中设置DataContext属性(查看App.axaml.cs)

3、Avalonia组成

(1)控制器

(2)输入

Avalonia输入系统使用直接事件路由事件来支持文本输入、焦点管理和鼠标定位。

(3)布局

  • Panel: Child controls are stacked on top of each other to fill the panel

  • DockPanel: Child controls are aligned to the edges of the panel

  • StackPanel: Child controls are stacked either vertically or horizontally

  • WrapPanel: Child controls are positioned in left-to-right order and wrapped to the next line when there are more controls on the current line than space allows

  • Grid: Child controls are positioned by rows and columns

  • Canvas: Child controls provide their own layout

(4)数据绑定

  • Multiple binding modes: one way, two way, one-time and one-way to source

(5)Graphics图集

(6)Animation动画

PS:文档地址:Getting Started - Avalonia UI

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值