Search code examples
c#wpfavaloniaui

C#/AvaloniaUI - OpenFolderDialog


I am using AvaloniaUI and I am trying to implement: http://avaloniaui.net/api/Avalonia.Controls/OpenFolderDialog/

What I want to achieve is when I press a button and it triggers openFold to show up a dialog making me choose a directory. Once I select a directory I want to store it's path in var result.

Here is my code:

public async Task openFold()
{
    var dialog = new OpenFolderDialog();
    var result = await dialog.ShowAsync();

    if (result != null)
    {
        await openFold(result);
    }
    Trace.WriteLine("DIR IS: " + result);
}

However I don't know how to pass the current main window to ShowAsync.

I am not sure that even if I pass the current window to ShowAsync it will show up a choose directory dialog. Am I doing it correctly ?


Solution

  • I've just checked an AvaloniaUI app where I run your code (except await openFold(result) since you didn't provide the implementation of that method).

    Nevertheless, everything works perfectly, I'm getting the folder selection window open, I can choose the folder and it is returned in result.

    It works both with the window parameter given and without, as it is optional.

    If the openFold() method is defined within the MainWindow class, you simply run
    var result = await dialog.ShowAsync(this);

    Should you want to place the method somewhere else, create a static accessor variable for the main window:
    public static MainWindow Instance;
    Assign it in the constructor:
    Instance = this;
    and call your folder selection window by:
    var result = await dialog.ShowAsync(MainWindow.Instance);

    Or did I misunderstood your problem?