首页 > 解决方案 > 如何防止 WPF 应用程序中的开始菜单固定

问题描述

使用WindowsAPICodePack.Shell我试图阻止用户将 WPF 应用程序固定到开始菜单和任务栏。我在 Windows 10 机器上执行此操作。

我尝试了两种方法,都阻止固定到任务栏,但仍然可以固定到开始菜单。

选项1

    public void PreventPin()
    {
        Window parentWindow = Window.GetWindow(ShellWindow);

        string appID = "UI";
        Guid propGuid = new Guid("{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}"); 

        PropertyKey idProperty = new PropertyKey(propGuid, 5);              // System.AppUserModel.ID
        PropertyKey preventTaskbarPinning = new PropertyKey(propGuid, 9);  // System.AppUserModel.PreventPinning
        PropertyKey preventStartPinning = new PropertyKey(propGuid, 12);  // System.AppUserModel.PreventPinning

        //Important: Set PreventPinning before ID
        WindowProperties.SetWindowProperty(parentWindow, preventTaskbarPinning, "True");
        WindowProperties.SetWindowProperty(parentWindow, preventStartPinning, "2");
        WindowProperties.SetWindowProperty(parentWindow, idProperty, appID);
    }

选项 2 - 在 HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileAssociation\NoStartPageAppUserModelIDs\ 的注册表中也存在 UI,并带有“UI”条目

    public void PreventPin()
    {
        Window parentWindow = Window.GetWindow(ShellWindow);

        string appID = "UI";
        Guid propGuid = new Guid("{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}"); 

        PropertyKey idProperty = new PropertyKey(propGuid, 5);              // System.AppUserModel.ID
        WindowProperties.SetWindowProperty(parentWindow, idProperty, appID);
    }

任何想法我做错了什么?

一些参考资料:

https://docs.microsoft.com/en-us/previous-versions//jj553605(v=vs.85)?redirectedfrom=MSDN

Windows 任务栏和开始菜单中固定 exe 的控制路径

https://docs.microsoft.com/en-us/windows/win32/shell/how-to-exclude-items-from-taskbar-pinning-and-recent-frequent-lists

标签: c#wpfwindows-10windows-api-code-pack

解决方案


我有个提议,设置你的主窗口,防止用户在windows上使用其他资源:

WindowStyle="None" WindowState="Maximized"

这不是最好的解决方案,它只会显示您的应用程序。


推荐阅读