问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

教程:在使用Windows应用SDK的未打包应用中使用引导程序API

创作时间:
作者:
@小白创作中心

教程:在使用Windows应用SDK的未打包应用中使用引导程序API

引用
1
来源
1.
https://learn.microsoft.com/zh-cn/windows/apps/windows-app-sdk/tutorial-unpackaged-deployment

本文将介绍如何在使用Windows应用SDK的未打包应用中使用引导程序API。通过本教程,你将学习如何在C#和C++项目中配置和使用引导程序API,以便在应用中动态依赖Windows应用SDK框架包。

C#项目配置

  1. 在Visual Studio中,创建一个新的C#控制台应用项目。将项目命名为“DynamicDependenciesTest”。创建项目后,应具有“Hello, World!”C#控制台应用。

  2. 接下来,配置项目:

  • 在“解决方案资源管理器”中,右击项目,然后选择“编辑项目文件”。

  • TargetFramework元素的值替换为目标框架名字对象。例如,如果应用目标是Windows 10版本2004,请使用以下项:

    <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
    
  • 保存并关闭项目文件。

  1. 将解决方案的平台更改为x64。.NET项目中的默认值为AnyCPU,但WinUI 3不支持该平台:
  • 选择生成>配置管理器
  • 选择“活动解决方案平台”下的下拉列表,单击“新建”选项以打开“新建解决方案平台”对话框。
  • 在“键入或选择新平台”下的下拉菜单中,选择“x64”。
  • 选择“确定”以关闭“新建解决方案平台”对话框。
  • 在“配置管理器”中,单击“关闭”。
  1. 在项目中安装Windows应用SDK NuGet包:
  • 在“解决方案资源管理器”中,右键单击“依赖项”节点,然后选择“管理NuGet包”。
  • NuGet程序包管理器窗口中,选择“浏览”选项卡,并安装Microsoft.WindowsAppSDK包。

使用引导程序API

现在,可以使用引导程序API(请参阅使用打包到外部位置或未打包的应用的Windows应用SDK运行时)动态依赖Windows应用SDK框架包。这使你能够在应用中使用Windows应用SDK API。

打开Program.cs代码文件,并将默认代码替换为以下代码,以调用Bootstrap.Initialize方法来初始化引导程序。此代码定义初始化引导程序时应用所依赖Windows应用SDK版本。

重要:需要编辑以下代码,以适应特定配置。请参阅Bootstrap.Initialize方法的参数的说明,以便可以指定已安装的Windows应用SDK版本之一。

using System;
using Microsoft.Windows.ApplicationModel.DynamicDependency;

namespace DynamicDependenciesTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Bootstrap.Initialize(0x00010002);
            Console.WriteLine("Hello, World!");
            // Release the DDLM and clean up.
            Bootstrap.Shutdown();
        }
    }
}

引导程序API是本机C/C++ API,可用于在应用中使用Windows应用SDK API。但在使用Windows应用SDK 1.0或更高版本的.NET应用中,可以将.NET包装器用于引导程序API。该包装器比直接调用本机C/C++函数更方便地在.NET应用中调用引导程序API。前面的代码示例在引导程序API的.NET包装器中调用Bootstrap类的静态InitializeShutdown方法。

演示资源加载

为了演示已正确加载Windows应用SDK运行时组件,请添加一些使用Windows应用SDK中的ResourceManager类加载字符串资源的代码:

  1. 将新的资源文件(.resw)添加到项目(保留默认名称)。

  2. 在编辑器中打开资源文件后,使用以下属性创建新的字符串资源:

  • 名称:消息
  • 值:Hello, resources!
  1. 保存资源文件。

  2. 打开Program.cs代码文件,并将行Console.WriteLine("Hello, World!");替换为以下代码:

    // Create a resource manager using the resource index generated during build.
    var manager = new Microsoft.Windows.ApplicationModel.Resources.ResourceManager("DynamicDependenciesTest.pri");
    // Look up a string in the .resw file using its name.
    Console.WriteLine(manager.MainResourceMap.GetValue("Resources/Message").ValueAsString);
    
  3. 单击“不调试启动”(或“开始调试”)生成并运行应用。应会看到字符串Hello, resources!已成功显示。

C++项目配置

按照以下说明配置与外部位置打包或解压缩的C++ WinUI 3项目:

  1. 在Visual Studio中创建一个新的C++控制台应用项目。将项目命名为“DynamicDependenciesTest”。创建项目后,应具有“Hello, World!”C++控制台应用。

  2. 接下来,在项目中安装Windows应用SDK NuGet包:

  • 在“解决方案资源管理器”中,右键单击“引用”节点,然后选择“管理NuGet包”。
  • NuGet程序包管理器窗口中,选择“浏览”选项卡,然后搜索Microsoft.WindowsAppSDK
  1. 现在,可以使用引导程序API(请参阅使用打包到外部位置或未打包的应用的Windows应用SDK运行时)来初始化应用中的Windows应用SDK运行时。这使你能够在应用中使用Windows应用SDK API。

  2. 将以下包含文件添加到DynamicDependenciesTest.cpp文件顶部。可以通过Windows应用SDK NuGet包获取mddbootstrap.h标头:

    #include <windows.h>
    #include <MddBootstrap.h>
    
  3. 接下来,在主方法的开头添加此代码,以调用MddBootstrapInitialize函数来初始化引导程序并处理任何错误。此代码定义初始化引导程序时应用所依赖Windows应用SDK版本。

    重要:需要编辑以下代码,以适应特定配置。请参阅MddBootstrapInitialize函数的参数的说明,以便可以指定已安装的Windows应用SDK版本之一。

    const UINT32 majorMinorVersion{ 0x00010002 };
    PCWSTR versionTag{ L"" };
    const PACKAGE_VERSION minVersion{};
    const HRESULT hr{ MddBootstrapInitialize(majorMinorVersion, versionTag, minVersion) };
    // Check the return code for errors. If there is an error, display the result.
    if (FAILED(hr))
    {
        wprintf(L"Error 0x%X in MddBootstrapInitialize(0x%08X, %s, %hu.%hu.%hu.%hu)\n",
            hr, majorMinorVersion, versionTag, minVersion.Major, minVersion.Minor, minVersion.Build, minVersion.Revision);
        return hr;
    }
    
  4. 最后,添加此代码以显示字符串Hello, World!,并调用MddBootstrapShutdown函数以取消初始化引导程序:

    std::cout << "Hello, World!\n";
    // Release the DDLM and clean up.
    MddBootstrapShutdown();
    
  5. 最终代码应如下所示:

    #include <iostream>
    #include <windows.h>
    #include <MddBootstrap.h>
    
    int main()
    {
        // Take a dependency on Windows App SDK Stable.
        const UINT32 majorMinorVersion{ 0x00010002 };
        PCWSTR versionTag{ L"" };
        const PACKAGE_VERSION minVersion{};
        const HRESULT hr{ MddBootstrapInitialize(majorMinorVersion, versionTag, minVersion) };
        // Check the return code. If there is a failure, display the result.
        if (FAILED(hr))
        {
            wprintf(L"Error 0x%X in MddBootstrapInitialize(0x%08X, %s, %hu.%hu.%hu.%hu)\n",
                hr, majorMinorVersion, versionTag, minVersion.Major, minVersion.Minor, minVersion.Build, minVersion.Revision);
            return hr;
        }
        std::cout << "Hello, World!\n";
        // Release the DDLM and clean up.
        MddBootstrapShutdown();
    }
    
  6. 单击“不调试启动”(或“开始调试”)生成并运行应用。

本文原文来自微软官方文档

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号