简体   繁体   English

如何在任务栏中将新的 WPF 窗口显示为新的应用程序/图标 (win10 c#wpf)

[英]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 .我试图在我的 WPF 应用程序中启动一个新窗口而不让它与主机应用程序堆叠,我在 WPF 中打开一个窗口时查看了新任务栏图标上的帖子,但该帖子似乎适用于 Windows 7,我我正在尝试使用那里提供的代码,但我有一个错误,说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.据我了解,该应用程序不会在任务栏中显示为新图标,除非它具有不同的进程 ID。 Is there any way I can have the new window not stack in the taskbar in windows 10?有什么办法可以让新窗口不在 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");
    }

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.您必须在窗口的SourceInitialized事件处理程序中更改应用程序 ID,因为新创建的 WPF 窗口还没有句柄,只有在演示源初始化该窗口时才能获取其句柄。 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在您的代码中,您尝试更改没有任何句柄的窗口的应用程序 ID(它为零),因此您观察到的错误

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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM