Skip to main content

XPF in Avalonia

Step 1: Update the Project File

Change the SDK in your Avalonia application to use the XPF SDK:

<Project Sdk="Xpf.Sdk/1.3.0">

And disable automatic XPF initialization:

<PropertyGroup>
<DisableAutomaticXpfInit>true</DisableAutomaticXpfInit>
</PropertyGroup>

Step 2: Add an XPF Application Class

Add an XpfApp class to your project which inherits from System.Windows.Application:

using System.Windows;

namespace MyAvaloniaApplication;

/// <summary>
/// Represents the XPF application.
/// </summary>
public partial class XpfApp : Application
{
}

Step 3: Initialize the XPF Application

In your Avalonia App.xaml.cs file, create an instance XpfApp in OnFrameworkInitializationCompleted:

public override void OnFrameworkInitializationCompleted()
{
new XpfApp();

// Existing Avalonia initialization here
}

Step 3: Add an XPF UserControl

Add an XPF UserControl to your application which contains the XPF content that you wish to host. For example:

<UserControl x:Class="MyAvaloniaApplication.MyXpfView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Button>Hello XPF!</Button>
</UserControl>
using System.Windows.Controls;

namespace MyAvaloniaApplication;

public partial class MyXpfView : UserControl
{
public MyXpfView()
{
InitializeComponent();
}
}

Step 4: Host the XPF UserControl

Intiantiate an XpfContainer to host the XPF content in an Avalonia control:

<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyAvaloniaApplication"
xmlns:xpf="clr-namespace:Atlantis;assembly=PresentationFramework"
x:Class="MyAvaloniaApplication.MainWindow">
<xpf:XpfContainer>
<local:MyXpfView/>
</xpf:XpfContainer>
</Window>