Skip to content

Upgrading to Avalonia v11

Damian edited this page Mar 29, 2024 · 9 revisions

This page will help you upgrade your application to the latest Avalonia v11.

Deprecations

https://github.com/AvaloniaUI/Avalonia/wiki/Breaking-Changes

  • DropDown -> ComboBox

DataTemplates and DataTypes

This is a breaking change that affects your Views.

Developers must define the DataType property of the DataTemplate and ItemTemplate elements.

Use CompileBindings

When using Compiled Bindings in your XAML, not only will your applications perform better, but it will also allow the compiler will throw an error immediately so that you do not get any surprises at runtime.

All child nodes will inherit this property, so you can enable it in your root node and disable it for a specific child if needed. Below are the 3 key properties which must be set to use in this fashion.

  1. xmlns:vm="using:NAMESPACE.OF.VIEWMODEL"
  2. x:CompileBindings="True"
  3. x:DataType="vm:YOURVIEWMODEL"

For more information, see the official documentation of Compiled Bindings to assist your project's needs better.

Sample:

<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:system="clr-namespace:System;assembly=mscorlib"
             xmlns:vm="using:SampleMvvmApp.ViewModels"
             x:Class="SampleMvvmApp.Views.DashboardView"
             x:CompileBindings="True"
             x:DataType="vm:DashboardViewModel">

AvaloniaLocator via ViewModels (MVVM)

IRuntimePlatform

Avalonia v0.10:

// Use the following when the `revision` wildcard is in use.
Version? version = System.Reflection.Assembly.GetExecutingAssembly()?.GetName().Version;
DateTime buildDate = version is not null ? new DateTime(2000, 1, 1).AddDays(version.Build).AddSeconds(version.Revision * 2) : default;
string ver = $"{version}{(version?.Revision != 0 ? $" ({buildDate})" : string.Empty)}";

string platform = "";
RuntimePlatformInfo? info = AvaloniaLocator.Current.GetService<IRuntimePlatform>()?.GetRuntimeInfo();
platform = info.Value.OperatingSystem.ToString();

Clipboard

Avalonia v0.10:

// Some ViewModel
  if (string.IsNullOrEmpty(text) || Application.Current is null || Application.Current.Clipboard is null)
    return;

  await Application.Current.Clipboard.SetTextAsync(text);

Avalonia v11.x

// SampleView.axaml.cs
public partial class SampleView : UserControl
{
    private readonly SampleViewModel? _vm;

    public DeviceView()
    {
        InitializeComponent();
        _vm = DataContext as SampleViewModel;
    }

    protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
    {
        base.OnAttachedToVisualTree(e);
        if (_vm is not null)
            _vm.HostWindow = TopLevel.GetTopLevel(this);
    }

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

// SampleViewModel
public DelegateCommand CmdCopyToClipboard => new(async () =>
{
  if (HostWindow is null || HostWindow.Clipboard is null)
    return;

  var contents = "Hello from Suess Labs and Prism.Avalonia!";
  await HostWindow.Clipboard.SetTextAsync(contents);
}

Acrylic Blur

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        ...
        TransparencyLevelHint="AcrylicBlur"
        Background="Transparent"
        Title="{Binding Title}">

  <!-- RowDefinitions="*,150,Auto" -->
  <Grid ColumnDefinitions="Auto,*" RowDefinitions="*,Auto">
    <ExperimentalAcrylicBorder IsHitTestVisible="False">
      <ExperimentalAcrylicBorder.Material>
        <ExperimentalAcrylicMaterial BackgroundSource="Digger"
                                     TintColor="LightGray"
                                     TintOpacity="0.1"
                                     MaterialOpacity="0.65" />
      </ExperimentalAcrylicBorder.Material>
    </ExperimentalAcrylicBorder>