Window resize/fullscreen event xamarin.mac

PJ 96 Reputation points
2021-05-01T16:28:36.993+00:00

Hello,

I'm building an application and I'd like to change some constraints on my storyboard when the user enters full screen. I'm thinking there should be some event I can subscribe to.

Here's the class for my main window. You can see I've override-ed the WillEnterFullScreen function, although the code isn't working (the console.writeline is never printed & breakpoint is never reached)

public partial class MainWindow : NSWindow
    {

        //in your window class 
        public new WindowDelegate Delegate
        {
            get { return (WindowDelegate)(NSWindowDelegate)base.Delegate; }
            set { base.Delegate = value; }
        }

        public MainWindow (IntPtr handle) : base (handle)
        {
        }
    }

    public class WindowDelegate : NSWindowDelegate
    {
        public event EventHandler Closing;

        public override void DidEndLiveResize(NSNotification notification)
        {
            Console.WriteLine("I am Resized");
        }


        public override void WillEnterFullScreen(NSNotification notification)
        {
            base.WillEnterFullScreen(notification);
        }

    }

I've been trying something like this too:

MainWindow.WillEnterFullScreenNotification += () => { ... }

and this

NSApplication.WindowResizedNotification += ... 

but I'm just getting errors.

I've had a look through the forums but haven't had much luck. Any help or advice would be much appreciated.

Thanks for your time.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
0 comments No comments
{count} votes

Accepted answer
  1. PJ 96 Reputation points
    2021-05-02T02:38:36.433+00:00

    I worked it out. Here's the answer for anyone looking:

    Step 1. Create two NSObjects in your viewcontroller class:

            NSObject NSWindowDidEnterFullScreenNotification;
            NSObject NSWindowDidExitFullScreenNotification;
    

    Step 2. In your ViewDidLoad() method add this:

                NSWindowDidEnterFullScreenNotification = NSNotificationCenter.DefaultCenter.AddObserver(new NSString("NSWindowDidEnterFullScreenNotification"), FullScreenObserver, null);
    
                NSWindowDidExitFullScreenNotification = NSNotificationCenter.DefaultCenter.AddObserver(new NSString("NSWindowDidExitFullScreenNotification"), ExitFullScreenObserver, null);
    

    Step 3. Then in your ViewController class add these two methods:

            public void ExitFullScreenObserver(NSNotification notify)
            {
             //... your code here 
                Console.WriteLine("full screen mode exited");
            }
    
            public void FullScreenObserver(NSNotification notify)
            {
             //... your code here 
                Console.WriteLine("full screen mode entered");
            }
    

    NB: this doesn't dispose of the NSObjects - for more info see User SushiHangover's Answer on StackOverflow: how-to-detect-resize-in-nsview-in-xamarin-mac

    0 comments No comments

0 additional answers

Sort by: Most helpful