🖥
WPF Developers Tips
Learn to leverage your existing knowledge of WPF to kick start your journey with Avalonia.
Avalonia is in general very similar to WPF, but you will find differences. Here are the most common:
If you use
Application.Current.Dispatcher
, you should use Dispatcher.UIThread
instead which provide similar facilities.WPF
Application.Current.Dispatcher.InvokeAsync(handler, DispatcherPriority.Background);
Avalonia
Dispatcher.UIThread.InvokeAsync(handler, DispatcherPriority.Background);
If you previously access dispatcher on the control itself, you should use global static instance
Dispatcher.UIThread
too.WPF
this.Dispatcher.InvokeAsync(handler, DispatcherPriority.Background);
Avalonia
Dispatcher.UIThread.InvokeAsync(handler, DispatcherPriority.Background);
WPF has uses
Visibility
property which can be Collapsed
, Hidden
or Visible
. Avalonia uses simpler and more intuitive model bool IsVisible
.WPF
<TextBox Name="MyTextbox" Text="{Binding ElementName=searchTextBox, Path=Text}"" />
Avalonia
<TextBox Name="MyTextbox" Text="{Binding #searchTextBox.Text}" />
There no events like
Loaded
/Unloaded
in Avalonia, but you can override OnAttachedToVisualTree
/OnDetachedFromVisualTree
on the control, to know when control attached to virtual tree, or detatched from it. Alternatively you can use TemplateApplied
instead of Loaded
event.WPF
Unloaded += OnControl_Unloaded;
Loaded += OnControl_Loaded;
private void OnControl_Loaded(object sender, RoutedEventArgs e)
{
// Handle control loaded event.
}
private void OnControl_Unloaded(object sender, RoutedEventArgs e)
{
// Handle control unload event.
}
Avalonia
TemplateApplied += OnControl_Loaded;
private void BuildControl_Loaded(object sender, RoutedEventArgs e)
{
}
// or
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
// Handle control loaded event.
}
// Use this instead of Unloaded event.
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
// Handle control unload event.
}
That mean that you cannot subscribe to tree attachment/detachment events for other controls.
Avalonia controls does not have
ToolTip
property like WPF. Instead you should use ToolTip.Tip
WPF
<Button ToolTip="Save file as..." />
Avalonia
<Button ToolTip.Tip="Save file as..." />
WPF
TextRunProperties.SetTextDecorations(TextDecorations.Underline);
Avalonia
TextRunProperties.Underline = true;
Last modified 14d ago