Visual-Studio/XCode Interface Builder, C Sharp App not relaunching its window

Alexander Zühr 21 Reputation points
2022-12-09T22:17:34.487+00:00

Hello,
i've programmed my first C# App with Visual-Studio and XCode Interface Builder on my Mac.
The app is running fine except 1 large problem.

After starting the app everything is running fine, i am able to minimize or maximize it, but if
i close the window of the program (red cross top left) i am not able to relaunch the window of the app.
On Macbooks apps dont really close and still are running after closing the window, if you click on every
other app in your dock, which is still running, the window of this program will reappear.

Sadly this isnt the case for my app, the window remains hidden and to get the app running again if
have to rightlick on it in the dock and force it to quit and restart it afterwards.
After restarting the window reappears and everything is working fine until i close the window again.

-------

I've tried the app in debug mode and also in release mode.
The app is correctly signed with my developer key.

I use an Apple M1 Pro and Visual Studio for Mac 17.4.1 and XCode 14.1.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,380 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,449 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
1,093 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,426 Reputation points Microsoft External Staff
    2022-12-13T04:44:44.117+00:00

    Hello @Alexander Zühr ,

    You are using Xamarin.Mac to build a Mac app, macOS uses Mutiple Document Interface and this is the behavior of macOS.
    You can use the native macOS API handler applicationShouldHandleReopen:hasVisibleWindows: method to reopen the closed window.

    **Override the NSApplicationDelegate in AppDelegate.cs **

    public override bool ApplicationShouldHandleReopen(NSApplication sender, bool hasVisibleWindows)  
          {  
                if (!hasVisibleWindows){  
                      var sb = NSStoryboard.FromName("Main", null);//Main.storyboard  
                      var controller = sb.InstantiateControllerWithIdentifier("MainWindow") as NSWindowController;// "MainWindow" is the Storyboard ID in WindowController of Main.storyboard.   
                controller.Window.MakeKeyAndOrderFront(this);//You can change this window(controller.Window) if you have another window in your project  
                }  
            return true;  
        }  
    

    Tips: The Storyboard ID is empty by default in WindowController of Main.storyboard, you can set one, refer to the screenshot at Windows in Xamarin.Mac - Xamarin | Microsoft Learn

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.