'SetForegroundWindow on macOS using C#

On Windows I can set a window to be in foreground using this code:

[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
...
var process = Process.Start(start);
IntPtr handle = process.MainWindowHandle;
SetForegroundWindow(handle);

How can this be achieved on macos with .net 6?



Solution 1:[1]

In AvaloniaUI you can do this in cross-platform fashion using Window.Topmost.

This is how you would create a window in XAML that will always be on top:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:AlwaysOnTop.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="AlwaysOnTop.Views.MainWindow"
        Topmost="True"
        Title="AlwaysOnTop">

</Window>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 frankenapps