美文网首页
Avalonia UI 11

Avalonia UI 11

作者: gruan | 来源:发表于2023-09-28 18:32 被阅读0次

几个优势:

  1. 自绘控件
  2. 支持 Windows, MAC, Linux, Android, Web
  3. 支持 Native AOT

比较

  1. 虽然控件不及 Flutter 的丰富, 但也是自绘的, 各个平台可以做到统一, 重要的是, 它是 .NET 的一员.
  2. 同时又能像 Xamarin / MAUI 一样, 支持多个平台,。因为是自绘控件, 界面又比 MAUI 统一, 而且 XAML 语法又和 WPF/Xamarin/MAUI 高度相似, 学习起来不费力气。
  3. 最重要的一点, 完全支持 Native AOT。 MAUI 也挺好的,但是 WIN 10 以前的系统不支持,WPF 也挺好的, 但是不支持 AOT。
  4. Avalonia UI 11 + .NET8 用 AOT 编译出来的 exe 文件成功运行于 WIN2008 R2 上。

知识点,备忘

        // 从当前控件获取 TopLevel。或者,您也可以使用 Window 引用。
        var topLevel = TopLevel.GetTopLevel(this);

        // 启动异步操作以打开对话框。
        var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
        {
            Title = "Open Text File",
            AllowMultiple = false
        });
<Image Source="avares://MyAssembly/Assets/icon.png"/>

通过在该项组中添加额外的<AvaloniaResource>元素来包含所需的任何文件

<ItemGroup>
  <AvaloniaResource Include="Assets\**"/>
</ItemGroup>

<Image Source="icon.png"/>
<Image Source="images/icon.png"/>
<Image Source="../icon.png"/>
<Image Source="/Assets/icon.png"/>

加载网络图片:
https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia

<!-- 使用CompiledBinding标记进行绑定 -->
<TextBox Text="{CompiledBinding LastName}" />
<!-- 这个命令将使用ReflectionBinding,因为它是默认值 -->
<Button Content="Send an E-Mail" Command="{Binding SendEmailCommand}" />
<!-- 我们使用ReflectionBinding -->
<Button Content="Send an E-Mail" Command="{ReflectionBinding SendEmailCommand}" />

全局编译绑定设置, 启用AOT 时, 最好使用 CompiledBinding, 否则会因为裁剪导致找不到属性的错误:

<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>

绑定到控件:
https://docs.avaloniaui.net/zh-Hans/docs/next/guides/data-binding/binding-to-controls

<TextBox Name="other">
<!-- 绑定到命名为 other 控件的 Text 属性 -->
<TextBlock Text="{Binding #other.Text}"/>
<TextBlock Text="{Binding Text, ElementName=other}"/>

$parent

<Border Tag="Hello World!">
  <TextBlock Text="{Binding $parent.Tag}"/>
</Border>

<Border Tag="Hello World!">
  <Border>
    <TextBlock Text="{Binding $parent[1].Tag}"/>
  </Border>
</Border>

索引从0开始,因此 $parent[0] 等同于 $parent

<Border Tag="Hello World!">
  <Decorator>
    <TextBlock Text="{Binding $parent[Border].Tag}"/>
  </Decorator>
</Border>

<Border Tag="Hello World!">
  <Border>
    <Decorator>
    <TextBlock Text="{Binding $parent[Border;1].Tag}"/>
    </Decorator>
  </Border>
</Border>
<TextBlock Text="{x:Static assets:Resources.GreetingText}"/>

在上面的示例中,GreetingText 是与 ResX 文件中的字符串对应的键。{x:Static} 标记扩展用于引用在 .NET 类中定义的静态属性,而在这种情况下,即资源文件(assets:Resources.GreetingText)。

  • 在WPF中的HierarchicalDataTemplate在Avalonia中被称为TreeDataTemplate

相关文章

网友评论

      本文标题:Avalonia UI 11

      本文链接:https://www.haomeiwen.com/subject/uoblbdtx.html