Lime
Lime

Reputation: 372

Avalonia SaveFileDialog freezes app, but OpenFileDialog works fine

I'm trying to open a SaveFileDialog from a ReactiveCommand in Avalonia.

public ReactiveCommand<Window, Unit> SaveFileAs { get; } = ReactiveCommand.Create((Window source) =>
{
    var path = new SaveFileDialog().ShowAsync(source).Result;
    // ...
});

The code above freezes as the dialog is closed. However, the OpenFileDialog works as expected.

public ReactiveCommand<Window, Unit> OpenFile { get; } = ReactiveCommand.Create((Window source) =>
{
    var paths = new OpenFileDialog().ShowAsync(source).Result;
    // ...
});

I also have applied [STAThread] to the main method (by default from the Avalonia MVVM template).

// Program.cs
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
    .StartWithClassicDesktopLifetime(args);

What is the cause for this peculiar behavior? What is the fix?

Upvotes: 0

Views: 593

Answers (1)

demonplus
demonplus

Reputation: 5801

I think freezing in your code is caused by Result. Try to switch to async/await for both dialogs:

var dlg = new SaveFileDialog();
var result = await dlg.ShowAsync(this);

You'll probably need to use CreateFromTask() instead of sync Create().

Upvotes: 2

Related Questions