桌面应用(例如使用 Windows 应用 SDK 的 WinUI 3 应用、Windows Presentation Foundation(WPF)应用或 Windows 窗体(WinForms)应用)可以通过使用打包的应用来固定附加磁贴(请参阅 从代码生成 MSIX 包)。 这以前被称为桌面桥。
重要
需要 Fall Creators Update:必须以 SDK 16299 为目标,并运行内部版本 16299 或更高版本来固定桌面桥应用中的辅助磁贴。
在 Windows 应用 SDK、WPF 或 WinForms 应用程序中添加辅助磁贴类似于纯 UWP 应用。 唯一的区别是必须指定主窗口句柄(HWND)。 这是因为固定磁贴时,Windows 会显示一个模式对话框,要求用户确认是否要固定磁贴。 如果桌面应用程序未使用所有者窗口配置 SecondaryTile 对象,则 Windows 不知道在何处绘制对话框,并且作将失败。
打包应用程序
要创建一个使用 WinUI 3 的 Windows 应用 SDK 应用程序,您必须使用打包应用程序来固定辅助磁贴。 如果从打包的应用模板开始,则无需执行额外的步骤来打包应用。
如果你使用的是 WPF 或 WinForms,并且尚未将应用打包到桌面桥中,则需要执行此作,然后才能使用任何 Windows 运行时 API(请参阅 从代码生成 MSIX 包)。
使用 IInitializeWithWindow 接口初始化并钉住辅助磁贴
注释
本部分适用于 WinUI 3,以及用于 .NET 6 或更高版本的 WPF/WinForms。
在项目文件中,将 TargetFramework 属性设置为可以访问 Windows 运行时 API 的值(请参阅 .NET 6 及更高版本:使用目标框架标识符选项)。 这包括访问 WinRT.Interop 命名空间(请参阅 从 .NET 应用程序中调用互操作 API)。 例如:
<PropertyGroup>
<!-- You can also target other versions of the Windows SDK and .NET; for example, "net8.0-windows10.0.19041.0" -->
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
</PropertyGroup>
像使用普通 UWP 应用一样初始化新的辅助磁贴对象。 若要了解有关创建和固定辅助磁贴的详细信息,请参阅 固定辅助磁贴。
// Initialize the tile with required arguments
var tile = new Windows.UI.StartScreen.SecondaryTile(
"myTileId5391",
"Display name",
"myActivationArgs",
new Uri("ms-appx:///Assets/Square150x150Logo.png"),
TileSize.Default);
检索窗口句柄,并使用该句柄初始化辅助磁贴对象。 在下面的代码中, this 是对 Window 对象的引用(无论是 WinUI 3 窗口、WPF 窗口还是 WinForms 窗口)。 有关详细信息,请参阅 检索窗口句柄 (HWND) 和 显示依赖于 CoreWindow 的 WinRT UI 对象。
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
WinRT.Interop.InitializeWithWindow.Initialize(tile, hWnd);
最后,请求像在普通 UWP 应用中一样固定磁贴。
// Pin the tile
bool isPinned = await tile.RequestCreateAsync();
// Here, update UI to reflect whether user can now either unpin or pin
注释
本部分适用于版本低于 5 的 WPF/WinForms。
使用 ComImport 和 Guid 属性声明 IInitializeWithWindow 接口。
[System.Runtime.InteropServices.ComImport]
[System.Runtime.InteropServices.Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
[System.Runtime.InteropServices.InterfaceType(
System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIUnknown
)]
public interface IInitializeWithWindow
{
void Initialize(IntPtr hWnd);
}
像使用普通 UWP 应用一样初始化新的辅助磁贴对象。 若要了解有关创建和固定辅助磁贴的详细信息,请参阅 固定辅助磁贴。
// Initialize the tile with required arguments
var tile = new Windows.UI.StartScreen.SecondaryTile(
"myTileId5391",
"Display name",
"myActivationArgs",
new Uri("ms-appx:///Assets/Square150x150Logo.png"),
TileSize.Default);
检索窗口句柄,并使用该句柄初始化辅助磁贴对象。 有关详细信息,请参阅 检索窗口句柄 (HWND) 和 显示依赖于 CoreWindow 的 WinRT UI 对象。
var wih = new System.Windows.Interop.WindowInteropHelper(this);
var hWnd = wih.Handle; // For a WinForms window object, access the NativeWindow.Handle property instead.
IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)tile;
initWindow.Initialize(hWnd);
最后,请求像在普通 UWP 应用中一样固定磁贴。
// Pin the tile
bool isPinned = await tile.RequestCreateAsync();
// Here, update UI to reflect whether user can now either unpin or pin
将以下内容添加到 pch.h 文件。
shobjidl.h 提供对 IInitializeWithWindow 接口的访问权限; Microsoft.UI.Xaml.Window.h 提供对 WinUI 3 Window 类的访问权限。
#include <shobjidl.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.UI.StartScreen.h>
#include <Microsoft.UI.Xaml.Window.h>
像使用普通 UWP 应用一样初始化新的辅助磁贴对象。 若要了解有关创建和固定辅助磁贴的详细信息,请参阅 固定辅助磁贴。
auto tile = Windows::UI::StartScreen::SecondaryTile(
L"myTileId5391",
L"Display name",
L"myActivationArgs",
Windows::Foundation::Uri(L"ms-appx:///Assets/Square150x150Logo.png"),
Windows::UI::StartScreen::TileSize::Default);
检索窗口句柄,并使用该句柄初始化辅助磁贴对象。 在下面的代码中, 是一个指向 WinUI 3 窗口 对象的 指针。 有关详细信息,请参阅 检索窗口句柄 (HWND) 和 显示依赖于 CoreWindow 的 WinRT UI 对象。
auto windowNative{ this->m_inner.as<::IWindowNative>() };
HWND hWnd{ 0 };
windowNative->get_WindowHandle(&hWnd);
tile.as<IInitializeWithWindow>()->Initialize(hWnd);
最后,请求像在普通 UWP 应用中一样固定磁贴。
// Pin the tile
bool isPinned = co_await tile.RequestCreateAsync();
// Here, update UI to reflect whether user can now either unpin or pin
发送磁贴通知
重要
需要 2018 年 4 月版本 17134.81 或更高版本:您必须运行内部版本 17134.81 或更高版本,才能从桌面桥应用发送磁贴或标记通知到辅助磁贴。 在此 .81 服务更新之前,从 Desktop Bridge 应用向辅助磁贴发送磁贴或标记通知时,会发生 0x80070490 元素未找到 异常。
发送磁贴或徽章通知与 UWP 应用相同。 请参阅 发送本地磁贴通知 以开始使用。
资源