【问题标题】:Win 11 : Pin Unpin a shortcut programmatically using C#Win 11:使用 C# 以编程方式固定取消固定快捷方式
【发布时间】:2023-02-21 05:58:21
【问题描述】:

C# 中是否有任何方法可以将应用程序固定/取消固定到 Windows 11 中的开始菜单和任务栏。 我正在使用 .NET Framework 4.8。

我可以使用提到的 Pin/Unpin API 将快捷方式固定/取消固定到 Win 10 中的任务栏和开始菜单位置 here

如何使用 C# 在 Windows 11 中实现快捷方式的固定/取消固定?

【问题讨论】:

    标签: c# .net windows windows-11


    【解决方案1】:

    执行此操作的“非官方”方式(您已链接)已经更改了几次,Microsoft 可能会继续打破这种方式以防止应用程序开发人员在未经用户同意的情况下这样做。理念是应用程序抽屉(开始菜单)是用户应该找到您的应用程序的地方。如果他们希望它更加突出(在任务栏上),则应该通过用户选择。

    在 Windows 10 和 11 中,有一个官方 API 要求用户将您的应用程序固定到开始菜单。 https://learn.microsoft.com/en-us/windows/apps/design/shell/pin-to-taskbar

    要使用它,您需要将 Windows TFM 设置为大于 10.0.16299。

    例如,在您的 csproj 中,您可以按如下方式设置 TFM

    <PropertyGroup>
        <TargetFramework>net6.0-windows10.0.17763</TargetFramework>
    </PropertyGroup>
    

    设置 TFM 后,您现在可以使用 WinRT API,例如 TaskbarManager

    一个例子:

    using Windows.Foundation.Metadata;
    using Windows.UI.Shell;
    
    if (ApiInformation.IsTypePresent("Windows.UI.Shell.TaskbarManager"))
    {
        var taskbarManager = TaskbarManager.GetDefault();
        bool isPinningAllowed = taskbarManager.IsPinningAllowed;
        bool isPinned = await TaskbarManager.GetDefault().IsCurrentAppPinnedAsync();
        if (isPinningAllowed && !isPinned)
        {
            // if pinning is allowed, and our app is not pinned, request to be pinned
            await taskbarManager.RequestPinCurrentAppAsync();
        }
    }
    

    当您致电RequestPinCurrentAppAsync 时,将向用户显示一个对话框,询问是否允许将您的应用程序固定到任务栏。

    【讨论】:

    • 谢谢,我需要通过传递应用程序的可执行路径来固定应用程序,并且在固定/取消固定期间没有弹出消息。
    猜你喜欢
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多