From 9c82843945f5475b1f8ba6f94adb2484bf33e7f0 Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Sun, 21 May 2023 00:01:16 +0600 Subject: [PATCH] Add samples how code looks in WPF and Avalonia --- misc/wpf/README.md | 112 ++++++++++++++++++++++++++++ misc/wpf/dependencyproperty.md | 26 +++++++ misc/wpf/itemscontrol.md | 12 ++- misc/wpf/propertychangedcallback.md | 53 +++++++++++++ 4 files changed, 202 insertions(+), 1 deletion(-) diff --git a/misc/wpf/README.md b/misc/wpf/README.md index 92c7c03..752d12a 100644 --- a/misc/wpf/README.md +++ b/misc/wpf/README.md @@ -51,3 +51,115 @@ Avalonia is in general very similar to WPF, but you will find differences. Here {% content-ref url="rendertransforms-and-rendertransformorigin.md" %} [rendertransforms-and-rendertransformorigin.md](rendertransforms-and-rendertransformorigin.md) {% endcontent-ref %} + +# Dispatcher changes + +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); +``` + +In more details, it's explained in the [Accessing the UI thread](../../guides/basics/accessing-the-ui-thread.md) article. + +# Visibility of elements + +WPF has uses `Visibility` property which can be `Collapsed`, `Hidden` or `Visible`. Avalonia uses simpler and more intuitive model `bool IsVisible`. + +# Binding + +**WPF** +``` + +``` + +**Avalonia** +``` + +``` + +# Handling attachment to visual tree + +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. + + +# Tooltips + +Avalonia controls does not have `ToolTip` property like WPF. Instead you should use `ToolTip.Tip` + +**WPF** +```csharp +