简体   繁体   English

Win 11:使用 C# 以编程方式固定取消固定快捷方式

[英]Win 11 : Pin Unpin a shortcut programmatically using C#

Is there any way in C# to pin/unpin an application to Startmenu and taskbar in Windows 11. I am using .NET Framework 4.8.在 C# 中有什么方法可以将应用程序固定/取消固定到 Windows 11 中的开始菜单和任务栏。我正在使用 .NET Framework 4.8。

I am able to pin/unpin shortcuts to Taskbar and Startmenu locations in Win 10 using the Pin/Unpin API mentioned here我可以使用此处提到的 Pin/Unpin API 将快捷方式固定/取消固定到 Win 10 中的任务栏和开始菜单位置

How can I achieve the pin/unpin of shortcuts in Windows 11 using C#?如何使用 C# 在 Windows 11 中实现快捷方式的固定/取消固定?

The "unofficial" way (which you linked) to do this has changed 5-6 times already, and Microsoft is probably going to keep changing it to try and prevent application developers from doing this without user consent.执行此操作的“非官方”方式(您链接的)已经更改了 5-6 次,并且 Microsoft 可能会继续更改它以尝试阻止应用程序开发人员在未经用户同意的情况下这样做。 The philosophy is that the application drawer (start menu) is where the user should find your app.理念是应用程序抽屉(开始菜单)是用户应该找到您的应用程序的地方。 If they want it to be more prominent, it's up to them.如果他们想让它更加突出,这取决于他们。

In Windows 10 and 11, there is an official API to ask the user to pin your app to the start menu.在 Windows 10 和 11 中,有一个官方 API 要求用户将您的应用程序固定到开始菜单。 https://learn.microsoft.com/en-us/windows/apps/design/shell/pin-to-taskbar https://learn.microsoft.com/en-us/windows/apps/design/shell/pin-to-taskbar

To use this you will need to set a windows TFM greater than 10.0.16299.要使用它,您需要将 Windows TFM 设置为大于 10.0.16299。

For example, in your csproj you can set the TFM as follows例如,在您的 csproj 中,您可以按如下方式设置 TFM

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

Once you've set your TFM, you can now use WinRT API's such as TaskbarManager .设置 TFM 后,您现在可以使用 WinRT API,例如TaskbarManager

An example:一个例子:

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

When you call RequestPinCurrentAppAsync , the user will be presented with a dialog asking for permission to pin your app to the taskbar.当您调用RequestPinCurrentAppAsync时,将向用户显示一个对话框,询问是否允许将您的应用程序固定到任务栏。

截屏

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

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