Search code examples
xamarinxamarin.formsxamarin.mac

Xamarin.Mac show default filename when using NSSavePanel


In a Xamarin.Forms application I wish to implement a file Save As operation in the Xamarin.Mac project. When showing the NSSavePanel, I wish to present the current file name as the default.

I have seen setNameFieldStringValue mentioned here and here but an equivalent method does not appear to be exposed in Xamarin.

Is it possible to do this?

Here's the code I'm working with.

public string GetSaveAsFilePathOrNull(string defaultFileName)
{
    var dlg = NSSavePanel.SavePanel;
    dlg.SetNameFieldStringValue(defaultFileName); // Compiler error, no such method

    if (dlg.RunModal() == 1)
    {
        var url = dlg.Url;

        if (url != null)
        {
            var path = url.Path;
            return path;
        }
    }

    return null;
}

Solution

  • The Xamarin.Mac C# normalization of the ObjC function NSSavePanel.SetNameFieldStringValue is a read/write property (NSSavePanel.NameFieldStringValue).

    var dlg = NSSavePanel.SavePanel;
    dlg.NameFieldStringValue = defaultFileName;