[macOS]/[WPF] How to handle ScrollView size when main app window size changes

May be applicable for iPAD split view scenarios too (not sure)

I have a ScrollView inside a ContentPage that fills the ContentPage with a margin of 25 on all sides.
The ContentPage occupies 100% of main app window.

In my ContentPage I have subscribed to "SizeChanged" event which get notification correctly.

When I drag the right window edge, I expect the scrollview to honor the right margin.
How do I resize the ScrollView to match the new window size? ForceLayout on ScrollView/ContentPage does not work.

Answers

  • AbhijitDeshpandeAbhijitDeshpande INMember ✭✭

    Additional question: How to handle screen resolution change (macOS / Windows)
    This happens when you connect your Mac or Windows machine to projector with different resolution

    I can handle the notification in CustomRenderer where I can get the new display size
    For macOS -->
    NotificationCenter.default.addObserver & NSNotification.Name.NSApplicationDidChangeScreenParameters

  • LandLuLandLu Member, Xamarin Team Xamurai

    Are you meaning this issue occurs on WPF projects? How did you manipulate your ScrollView? If you set this ScrollView as your content page's content, it will follow the window's size changing.
    Please post your layout code.

  • Thanks. Let me create a sample project highlighting the issue. Currently on MacOS

  • I found the solution.
    In function "DidFinishLaunching" (AppDelegate.cs), subscribed to DidResize.
    Inside DidResize handler, called ForceLayout of the main page

        public override void DidFinishLaunching(NSNotification notification)
        {
            ...
            if(null != _window)
            {
                _window.DidResize += _window_DidResize;
            }
        }
    
        public override void WillTerminate(NSNotification notification)
            {
                if (null != _window)
                {
                    _window.DidResize -= _window_DidResize;
                }
            }
    
        void _window_DidResize(object sender, System.EventArgs e)
            {
            // app is the Application object.
                app?.MainPage?.ForceLayout();
            }
    
Sign In or Register to comment.