














<!--WPF-->
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
</Window>
<!--Avalonia-->
<Window xmlns="https://github.com/avaloniaui">
</Window>
<!--WPF-->
xmlns:local="clr-namespace:myApp"
<!--Avalonia(推荐)-->
xmlns:local="using:myApp"
| WPF(Trigger) | Avalonia(选择器+伪类) |
|---|---|
| Style.Triggers | 伪类 :pointerover,:pressed,:disabled |
| DataTrigger | 绑定伪类[(vm:MyViewModel.IsActive)] |
| EventTrigger | Style.Animations |
| VisualStateManager | 伪类系统 |
| 示例对比 |
<!--WPF DataTrigger-->
<DataTrigger Binding="{Binding IsError}" Value="True">
<Setter Property=Foreground" Value="Red" />
</DataTrigger>
<!--Avalonia:绑定伪类-->
<Style Selector="Border[IsError=true]">
<Setter Property="BorderBrush" Value="Red" />
</Style>
<!--WPF-->
<Style TargetType="Button">
<Setter Property="Background" Value="Blue" />
</Style>
<!--Avalonia-->
<Style Selector="Button">
<Setter Property="Background" Value="Blue" />
</Style>
<!--带有类名的样式 类似WPF中指定了Key-->
<Style Selector="Button.primary">
<Setter Property="Background" Value="Blue" />
</Style>
<!--使用时-->
<Button Class="primary" Content="确定" />
Avalonia 的样式选择器会自动叠加,不需要BaseOn显式继承
| WPF | Avalonia | 说明 |
|---|---|---|
| DependencyProperty | StyledProperty | 依赖属性:支持样式、动画、继承 |
| - | DirectProperty | 性能更好,但不支持样式、动画 |
| DependencyProperty.Register() | AvaloniaProperty.Register<TOwner,TValue> | 泛型注册 |
//WPF
public static readonly DependencyProperty TextProperty=DependencyProperty.Register(nameof(Text),typeof(string),typeof(MyControl),new PropertyMetadata(""));
//Avalonia
public static readonly StyledProperty<string> TextProperty=AvaloniaProperty.Register<MyControl,string>(nameof(Text),defaultValue:"");
坑:想给PasswordBox.Password做双向绑定,结果永远是空
解决方案:用附加属性封装,或直接用事件处理
| WPF | Avalonia | 说明 |
|---|---|---|
| {Binding RelativeSource={RelativeSource Self}} | 绑定自身 | |
| {Binding RelativeSource={RelativeSource AncestorType=Grid}} | 绑定父级 | |
| 绑定控件 | ||
| {Binding RelativeSource={RelativeSource TemplateParent}} | 模板绑定 |
Avalonia 支持 CompiledBinding,编译期就能检查绑定错误,强烈建议开启
<Window x:CompileBinding="True">
{Binding User.Name()} 多打了(),数据不显示
注意: 绑定属性不能加括号,绑定方法才加括号
| 控件 | 说明 |
|---|---|
| DataGrid | 需要安装Avalonia.Controls.DataGrid Nuget 包 |
| TreeView | 内置 HierarchicalDataTemplate 改叫TreeDataTemplate |
| WPF | Avalonia 替代方案 |
|---|---|
| ListView | 用ListBox+ItemTemplate |
| StatusBar | 自己用Panel样式实现 |
| RichTextBox | 用第三方编辑器 |
| RoutedCommand | 用ICommand实现(推荐CommunityToolkit.Mvvm) |
| WindowsFormsHost | 不支持,必须用Avalonia原生控件 |
<!--WPF-->
<Button ToolTip="提示文字" />
<!--Avalonia-->
<Button ToolTip.Tip="提示文字" />
| WPF | Avalonia | 说明 |
|---|---|---|
| Visibility.Visible | IsVisible="True" | 显示 |
| Visibility.Collapsed | IsVisible="False" | 隐藏且不占空间 |
| Visibility.Hidden | Opcity="0" | 隐藏但占用空间 |
<!--WPF-->
<Image Source="pack://application:,,,/Assets/logo.png" />
<!--Avalonia-->
<Image Source="avares://YourApp/Assets/logo.png" />
坑: 新建的XAML 资源文件默认Build Action 是None,启动报错“找不到资源”
解决: 右键文件-属性-生成操作-选择AvaloniaResource
坑: WPF会自动查找后面的资源,Avalonia不会
解决: 资源字典合并按依赖顺序排列,或改用DynamicResource
坑: Linux 上StackPanel 嵌套3层以上,滚动时CPU飙升
建议:
坑: SizeToContent="WidthAndHeight" 在Linux上窗口尺寸跑偏
建议: Linux上手动设置固定尺寸,加MaxWidth /MaxHeight 限制
坑: TextWrapping="Wrap" 在Linux 上可能不生效
解决:
坑: DPI不同导致Canvas坐标偏移
建议: 优先用Grid+Margin 相对布局,避免Canvas 做复杂布局
坑: Windows上的“微软雅黑”在Linux/MacOS 上乱码
解决方案:
<FontFamily x:Key="DefaultFont">
avares://YourApp/Fonts/微软雅黑.ttf#微软雅黑,scans-serif
</FontFamily>
坑: 16位深度PNG 在macOS上不显示
建议: 统一用8位PNG/JPG
解决:
坑: Avalonia默认切换Tab 时卸载内容以节省内存,输入的内容会丢失
解决:
<TabControl KeepAlive="True" />
坑: Popup 直接放在Window下,StaysOpen="False" 不生效
解决: 把Popup放在Grid /StackPanel 等容器内
Collection.Add(item) 抛异常解决:
Application.Current.Dispatcher.Invoke(()=>collection.Add(item))
| WPF | Avalonia |
|---|---|
| Dispatcher.Invoke() | Dispatcher.UIThread.InvokeAsync() |
| Dispatcher.BeginInvoke() | Dispatcher.UIThread.Post() |
| Dispatcher.CheckAccess() | Dispacher.UIThread.CheckAccess() |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。