Primary WPF APIs:
DispatcherDispatcherTimerBackgroundWorkerPrimary Avalonia APIs:
Dispatcher.UIThreadDispatcherTimerTask.Run + dispatcher marshaling| WPF | Avalonia |
|---|---|
Dispatcher.Invoke |
Dispatcher.UIThread.InvokeAsync |
Dispatcher.BeginInvoke |
Dispatcher.UIThread.Post |
DispatcherTimer |
DispatcherTimer |
BackgroundWorker |
Task.Run + progress callbacks to UI thread |
WPF C#:
Dispatcher.BeginInvoke(() => Status = "Running");
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
timer.Tick += (_, _) => Clock = DateTime.Now.ToString("T");
timer.Start();
Avalonia XAML:
<StackPanel xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:MyApp.ViewModels"
x:DataType="vm:RuntimeViewModel"
Spacing="8">
<TextBlock Text="{CompiledBinding StatusText}" />
<TextBlock Text="{CompiledBinding ClockText}" />
</StackPanel>
using System;
using System.Threading.Tasks;
using Avalonia.Threading;
Dispatcher.UIThread.Post(() => viewModel.StatusText = "Running");
var timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(1)
};
timer.Tick += (_, _) => viewModel.ClockText = DateTime.Now.ToString("T");
timer.Start();
await Task.Run(() =>
{
var result = Compute();
Dispatcher.UIThread.Post(() => viewModel.ApplyResult(result));
});
Dispatcher.UIThread for all UI mutations.Wait, Result) on UI thread.