1

I understand that XCode supports AutoSave for remembering window position. I see it freely documented.

What about Xamarin Forms 5, when the platform is macOS?

This is some of my code, if it helps:

public class AppDelegate : FormsApplicationDelegate
{
    NSWindow window;
    public AppDelegate()
    {
        var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;

        var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
        window = new NSWindow(rect, style, NSBackingStore.Buffered, false)
        {
            Title = "Elderly and Infirm Rota",
            TitleVisibility = NSWindowTitleVisibility.Visible
        };

        InstallApplicationFiles();
    }

Update

In the comments I also referred to limiting the size of the window. I have now worked out that bit (NSWindow.MinSize):

NSWindow window;
public AppDelegate()
{
    var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;

    var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
    window = new NSWindow(rect, style, NSBackingStore.Buffered, false)
    {
        Title = "Elderly and Infirm Rota",
        TitleVisibility = NSWindowTitleVisibility.Visible,
        MinSize = rect.Size
    };

    InstallApplicationFiles();
}

Now I have to address the window size / position and remembering it.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    Forms itself won't do it, but you should be able to use the Mac API from a custom renderer or dependency service to do it – Jason Feb 20 '21 at 19:16
  • @Jason I have used neither of those features. I will have to try and find a good tutorial for both. – Andrew Truckle Feb 20 '21 at 21:35
  • @Jason I see this but it does cover macOS: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/contentpage – Andrew Truckle Feb 20 '21 at 21:44
  • I am still struggling with this. Also applicable is the minimum allowed window size. – Andrew Truckle Feb 22 '21 at 12:09
  • 1
    you haven't posted any code or provided any detail about what you're trying to do or which APIs you're using – Jason Feb 22 '21 at 13:22
  • @Jason That is because I don't really know which part of this we are looking at. I have included the Main.CS contents from the MacOS project. I don't know if this was the code needed. – Andrew Truckle Feb 22 '21 at 13:32
  • @Jason Put simply - remember the window position and state (and in addition, limit the minimum window size) – Andrew Truckle Feb 22 '21 at 13:34
  • "I understand that XCode supports AutoSave" - I assume this means you have done some research and identified the relevant APIs needed to support this feature. – Jason Feb 22 '21 at 13:49
  • @Jason I can't find that link now. But I did find this https://developer.apple.com/documentation/appkit/nswindow#//apple_ref/doc/uid/TP40004151 but it is on about NSWindow and I don't even know if this is the right path I should be taking to resolve this. – Andrew Truckle Feb 22 '21 at 13:56
  • @Jason I made some progress (updated question). – Andrew Truckle Feb 22 '21 at 14:59

1 Answers1

1

Turns out it was not too hard. Visual Studio for Mac is missing some settings (I think) that are described for Xcode. See: frameAutoSaveName

The answers provided here were also useful. Part of the problem for me was having the right terms to search for an answer and in this case NSWindow was important.

In my macOS AppDeletegate constructor I now do this:

public AppDelegate()
{
    var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;

    var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
    window = new NSWindow(rect, style, NSBackingStore.Buffered, false)
    {
        Title = NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleDisplayName").ToString(),
        TitleVisibility = NSWindowTitleVisibility.Visible,
        MinSize = rect.Size,
        FrameAutosaveName = "xxxxx"
    };

    InstallApplicationFiles();
}

Notice the additional setting of the FrameAutosaveName property:

FrameAutosaveName = "xxxxx"

Now the window position is remembers when I close the application.


Please let me know if there was a way to set this directly in Visual Studio for Mac GUI.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164