Search code examples
c#wpfwindows-10taskbar

How to display a new WPF window as a new app/icon in the taskbar (win10 c#wpf)


I am trying to launch a new window in my WPF app without having it stack with the host application, I have had a look at the post on New taskbar icon when opening a window in WPF but that post seems to be for windows 7, I am trying to use the code provided there but I have an error saying The value does not fall within the expected range. From my understanding the app will not show as a new icon in the taskbar unless it has a different process ID. Is there any way I can have the new window not stack in the taskbar in windows 10?

Here is what I have tried

using Microsoft.WindowsAPICodePack.Taskbar;
public void App_Startup(object sender, StartupEventArgs e)
    {
        TaskbarManager.Instance.SetApplicationIdForSpecificWindow(new WindowInteropHelper(new window2()).Handle, "Gx3OptimisationWindow");
    }

Solution

  • You have to change the application ID in the SourceInitialized event handler of the window, because a newly created WPF window has no handle yet and only gets its handle when the presentation source initializes that window. In your code, you try to change the application ID of the window that doesn't have any handle (it is zero), thus the error you observe

    So instead of:

    public void App_Startup(object sender, StartupEventArgs e)
    {
        TaskbarManager.Instance.SetApplicationIdForSpecificWindow(new WindowInteropHelper(new window2()).Handle, "Gx3OptimisationWindow");
    }
    

    do this:

    class Window2
    {
        public Window2()
        {
            InitializeComponent();
            SourceInitialized += (s, e) =>
                TaskbarManager.Instance.SetApplicationIdForSpecificWindow(
                    new WindowInteropHelper(this).Handle,
                    "Gx3OptimisationWindow");
        }
    }