PackageManager.AddPackageAsync 方法

定义

重载

AddPackageAsync(Uri, IIterable<Uri>, DeploymentOptions)

使用指定的部署选项为当前用户添加包 (main包) 及其依赖项包。

AddPackageAsync(Uri, IIterable<Uri>, DeploymentOptions, PackageVolume)

使用指定的部署选项,将包 (main包) 及其依赖项包添加到当前用户的指定卷。

AddPackageAsync(Uri, IIterable<Uri>, DeploymentOptions, PackageVolume, IIterable<String>, IIterable<Uri>)

使用指定的部署选项,将 及其依赖项包添加到当前用户的指定卷。

AddPackageAsync(Uri, IIterable<Uri>, DeploymentOptions, PackageVolume, IIterable<String>, IIterable<Uri>, IIterable<Uri>)

使用指定的部署选项为当前用户添加包 (main包) 及其依赖项包。

AddPackageAsync(Uri, IIterable<Uri>, DeploymentOptions)

使用指定的部署选项为当前用户添加包 (main包) 及其依赖项包。

public:
 virtual IAsyncOperationWithProgress<DeploymentResult ^, DeploymentProgress> ^ AddPackageAsync(Uri ^ packageUri, IIterable<Uri ^> ^ dependencyPackageUris, DeploymentOptions deploymentOptions) = AddPackageAsync;
/// [Windows.Foundation.Metadata.Overload("AddPackageAsync")]
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> AddPackageAsync(Uri const& packageUri, IIterable<Uri> const& dependencyPackageUris, DeploymentOptions const& deploymentOptions);
[Windows.Foundation.Metadata.Overload("AddPackageAsync")]
public IAsyncOperationWithProgress<DeploymentResult,DeploymentProgress> AddPackageAsync(System.Uri packageUri, IEnumerable<System.Uri> dependencyPackageUris, DeploymentOptions deploymentOptions);
function addPackageAsync(packageUri, dependencyPackageUris, deploymentOptions)
Public Function AddPackageAsync (packageUri As Uri, dependencyPackageUris As IEnumerable(Of Uri), deploymentOptions As DeploymentOptions) As IAsyncOperationWithProgress(Of DeploymentResult, DeploymentProgress)

参数

packageUri
Uri Uri

要添加的包的 URI。 URI 必须遵循 file://) (文件 URI 方案,因为唯一受支持的 URI 方案是本地文件路径和本地网络路径。

dependencyPackageUris

IIterable<Uri>

IEnumerable<Uri>

要添加的依赖项包的 URI。 如果没有依赖项包,或者如果已注册依赖项包,则此参数可以为 null。

deploymentOptions
DeploymentOptions

DeploymentOptions 枚举中的枚举值的按位组合。 ForceApplicationShutdownNone 是此方法的唯一有效选项。 指定任何其他选项会导致E_INVALIDARG返回值。

返回

部署请求的状态。 DeploymentResult 包含部署操作的最终返回值(完成后)。 DeploymentProgress 可用于获取整个部署操作过程中的完成百分比。

属性

Windows 要求

应用功能
packageManagement

示例

以下示例使用 PackageManager.AddPackageAsync 方法安装没有依赖项或已安装依赖项的包。 请注意,在示例中,main包的路径作为参数传递。 AddPackageAsync 返回可用于管理异步操作的对象。 该示例使用 Completed 属性设置 委托 ,并检查 Status 属性以确定部署操作的状态。 如果状态为 “错误”,则示例调用 GetResults 方法以获取其他错误信息。

using Windows.Foundation;
using Windows.Management.Deployment;

public static int Main(string[] args)
{
    string inputPackageUri = args[0];
    int returnValue=0;

    Uri packageUri = new Uri(inputPackageUri);            

    PackageManager packageManager = new PackageManager();

    IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> deploymentOperation = 
        packageManager.AddPackageAsync(
            packageUri, 
            null, 
            DeploymentOptions.None);

    // This event is signaled when the operation completes
    ManualResetEvent opCompletedEvent = new ManualResetEvent(false); 

    // Define the delegate using a statement lambda
    deploymentOperation.Completed = (depProgress, status) => { opCompletedEvent.Set(); };

    // Wait until the operation completes
    opCompletedEvent.WaitOne();

    // Check the status of the operation
    if (deploymentOperation.Status == AsyncStatus.Error)
    {
        DeploymentResult deploymentResult = deploymentOperation.GetResults();
        Console.WriteLine("Error code: {0}", deploymentOperation.ErrorCode);
        Console.WriteLine("Error text: {0}", deploymentResult.ErrorText);
        returnValue = 1;
    }
    else if (deploymentOperation.Status == AsyncStatus.Canceled)
    {
        Console.WriteLine("Installation canceled");
    }
    else if (deploymentOperation.Status == AsyncStatus.Completed)
    {
        Console.WriteLine("Installation succeeded");
    }
    else
    {
        returnValue = 1;
        Console.WriteLine("Installation status unknown");
    }

    return returnValue;
}

另请参阅 Visual Studio 对 C++/WinRT 的支持

// main.cpp : In Visual Studio, create a new Windows Console Application (C++/WinRT).
#include "pch.h"

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Management.Deployment.h>
#include <iostream>

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Management::Deployment;

int wmain(int /* argc */, wchar_t *argv[], wchar_t * /* envp[] */)
{
    winrt::init_apartment();

    int returnValue{ 0 };

    std::wstring packageUriString{ argv[1] };
    Uri packageUri{ packageUriString };
    PackageManager packageManager;

    auto deploymentOperation{ packageManager.AddPackageAsync(packageUri, nullptr, DeploymentOptions::None) };
    deploymentOperation.get();

    // Check the status of the operation
    if (deploymentOperation.Status() == AsyncStatus::Error)
    {
        auto deploymentResult{ deploymentOperation.GetResults() };
        std::wcout << L"Error code: " << deploymentOperation.ErrorCode() << std::endl;
        std::wcout << L"Error text: " << deploymentResult.ErrorText().c_str() << std::endl;
        returnValue = 1;
    }
    else if (deploymentOperation.Status() == AsyncStatus::Canceled)
    {
        std::wcout << L"Installation canceled" << std::endl;
    }
    else if (deploymentOperation.Status() == AsyncStatus::Completed)
    {
        std::wcout << L"Installation succeeded" << std::endl;
    }
    else
    {
        std::wcout << L"Installation status unknown" << std::endl;
        returnValue = 1;
    }
    return returnValue;
}
using namespace Windows::Foundation;
using namespace Windows::Management::Deployment;

[STAThread]
int __cdecl main(Platform::Array<String^>^ args)
{  
    String^ inputPackageUri = args[1];
    int returnValue=0;

    Uri^ packageUri = ref new Uri(inputPackageUri);

    PackageManager^ packageManager = ref new PackageManager();

    auto deploymentOperation = packageManager->AddPackageAsync(
        packageUri, 
        nullptr, 
        DeploymentOptions::None);

    DeploymentResult^ deploymentOperationResult;

    // This event is signaled when the operation completes
    opCompletedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

    // Define the delegate
    deploymentOperation->Completed = 
        ref new AsyncOperationWithProgressCompletedHandler<DeploymentResult^, DeploymentProgress>(
        [&](IAsyncOperationWithProgress<DeploymentResult^, DeploymentProgress>^ operation, AsyncStatus)
    {
        SetEvent(opCompletedEvent);
    });

    // Wait until the operation completes
    WaitForSingleObject(opCompletedEvent, INFINITE);

    // Check the status of the operation
    if ( deploymentOperation->Status == AsyncStatus::Error )
    {
        auto deploymentResult = deploymentOperation->GetResults();
        wcout << L"Error code: " << deploymentOperation->ErrorCode.Value << endl;
        wcout << L"Error text: " << deploymentResult->ErrorText->Data() << endl;
        returnValue = 1;
    }
    else if ( deploymentOperation->Status == AsyncStatus::Canceled )
    {
        wcout << L"Installation canceled" << endl;
    }
    else if ( deploymentOperation->Status == AsyncStatus::Completed )
    {
        wcout << L"Installation succeeded" << endl;
    }
    else
    {
        wcout << L"Installation status unknown" << endl;
        returnValue = 1;
    }
    return returnValue;
}

另请参阅

适用于

AddPackageAsync(Uri, IIterable<Uri>, DeploymentOptions, PackageVolume)

使用指定的部署选项,将包 (main包) 及其依赖项包添加到当前用户的指定卷。

public:
 virtual IAsyncOperationWithProgress<DeploymentResult ^, DeploymentProgress> ^ AddPackageAsync(Uri ^ packageUri, IIterable<Uri ^> ^ dependencyPackageUris, DeploymentOptions deploymentOptions, PackageVolume ^ targetVolume) = AddPackageAsync;
/// [Windows.Foundation.Metadata.Overload("AddPackageToVolumeAsync")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> AddPackageAsync(Uri const& packageUri, IIterable<Uri> const& dependencyPackageUris, DeploymentOptions const& deploymentOptions, PackageVolume const& targetVolume);
[Windows.Foundation.Metadata.Overload("AddPackageToVolumeAsync")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperationWithProgress<DeploymentResult,DeploymentProgress> AddPackageAsync(System.Uri packageUri, IEnumerable<System.Uri> dependencyPackageUris, DeploymentOptions deploymentOptions, PackageVolume targetVolume);
function addPackageAsync(packageUri, dependencyPackageUris, deploymentOptions, targetVolume)
Public Function AddPackageAsync (packageUri As Uri, dependencyPackageUris As IEnumerable(Of Uri), deploymentOptions As DeploymentOptions, targetVolume As PackageVolume) As IAsyncOperationWithProgress(Of DeploymentResult, DeploymentProgress)

参数

packageUri
Uri Uri

要添加的包的 URI。 URI 必须遵循 file://) (文件 URI 方案,因为唯一受支持的 URI 方案是本地文件路径和本地网络路径。

dependencyPackageUris

IIterable<Uri>

IEnumerable<Uri>

要添加的依赖项包的 URI。 如果没有依赖项包,或者如果已注册依赖项包,则此参数可以为 null。

deploymentOptions
DeploymentOptions

DeploymentOptions 枚举中的枚举值的按位组合。 ForceApplicationShutdownNone 是此方法的唯一有效选项。 指定任何其他选项会导致E_INVALIDARG返回值。

targetVolume
PackageVolume

将包添加到的卷。

返回

部署请求的状态。 DeploymentResult 包含部署操作的最终返回值(完成后)。 DeploymentProgress 可用于获取整个部署操作过程中的完成百分比。

属性

Windows 要求

应用功能
packageManagement

另请参阅

适用于

AddPackageAsync(Uri, IIterable<Uri>, DeploymentOptions, PackageVolume, IIterable<String>, IIterable<Uri>)

使用指定的部署选项,将 及其依赖项包添加到当前用户的指定卷。

public:
 virtual IAsyncOperationWithProgress<DeploymentResult ^, DeploymentProgress> ^ AddPackageAsync(Uri ^ packageUri, IIterable<Uri ^> ^ dependencyPackageUris, DeploymentOptions deploymentOptions, PackageVolume ^ targetVolume, IIterable<Platform::String ^> ^ optionalPackageFamilyNames, IIterable<Uri ^> ^ externalPackageUris) = AddPackageAsync;
/// [Windows.Foundation.Metadata.DefaultOverload]
/// [Windows.Foundation.Metadata.Overload("AddPackageToVolumeAndOptionalPackagesAsync")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> AddPackageAsync(Uri const& packageUri, IIterable<Uri> const& dependencyPackageUris, DeploymentOptions const& deploymentOptions, PackageVolume const& targetVolume, IIterable<winrt::hstring> const& optionalPackageFamilyNames, IIterable<Uri> const& externalPackageUris);
[Windows.Foundation.Metadata.DefaultOverload]
[Windows.Foundation.Metadata.Overload("AddPackageToVolumeAndOptionalPackagesAsync")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperationWithProgress<DeploymentResult,DeploymentProgress> AddPackageAsync(System.Uri packageUri, IEnumerable<System.Uri> dependencyPackageUris, DeploymentOptions deploymentOptions, PackageVolume targetVolume, IEnumerable<string> optionalPackageFamilyNames, IEnumerable<System.Uri> externalPackageUris);
function addPackageAsync(packageUri, dependencyPackageUris, deploymentOptions, targetVolume, optionalPackageFamilyNames, externalPackageUris)
Public Function AddPackageAsync (packageUri As Uri, dependencyPackageUris As IEnumerable(Of Uri), deploymentOptions As DeploymentOptions, targetVolume As PackageVolume, optionalPackageFamilyNames As IEnumerable(Of String), externalPackageUris As IEnumerable(Of Uri)) As IAsyncOperationWithProgress(Of DeploymentResult, DeploymentProgress)

参数

packageUri
Uri Uri

要添加的源包的 URI。 URI 必须遵循 file://) (文件 URI 方案,因为唯一受支持的 URI 方案是本地文件路径和本地网络路径。

dependencyPackageUris

IIterable<Uri>

IEnumerable<Uri>

要添加的依赖项包的 URI。 如果没有依赖项包,或者如果已注册依赖项包,则此参数可以为 null。

deploymentOptions
DeploymentOptions

DeploymentOptions 枚举中的枚举值的按位组合。 ForceApplicationShutdownNone 是此方法的唯一有效选项。 指定任何其他选项会导致E_INVALIDARG返回值。

targetVolume
PackageVolume

将包添加到的卷。

optionalPackageFamilyNames

IIterable<String>

IEnumerable<String>

IIterable<Platform::String>

IIterable<winrt::hstring>

要注册main捆绑包中的包系列名称。

externalPackageUris

IIterable<Uri>

IEnumerable<Uri>

要注册main捆绑包中其他包的 URI。

返回

整个部署操作过程中的 DeploymentProgress 完成百分比。

属性

Windows 要求

设备系列
Windows 10 Creators Update (在 10.0.15063.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v4.0 中引入)

另请参阅

适用于

AddPackageAsync(Uri, IIterable<Uri>, DeploymentOptions, PackageVolume, IIterable<String>, IIterable<Uri>, IIterable<Uri>)

使用指定的部署选项为当前用户添加包 (main包) 及其依赖项包。

public:
 virtual IAsyncOperationWithProgress<DeploymentResult ^, DeploymentProgress> ^ AddPackageAsync(Uri ^ packageUri, IIterable<Uri ^> ^ dependencyPackageUris, DeploymentOptions options, PackageVolume ^ targetVolume, IIterable<Platform::String ^> ^ optionalPackageFamilyNames, IIterable<Uri ^> ^ packageUrisToInstall, IIterable<Uri ^> ^ relatedPackageUris) = AddPackageAsync;
/// [Windows.Foundation.Metadata.Overload("AddPackageToVolumeAndRelatedSetAsync")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> AddPackageAsync(Uri const& packageUri, IIterable<Uri> const& dependencyPackageUris, DeploymentOptions const& options, PackageVolume const& targetVolume, IIterable<winrt::hstring> const& optionalPackageFamilyNames, IIterable<Uri> const& packageUrisToInstall, IIterable<Uri> const& relatedPackageUris);
[Windows.Foundation.Metadata.Overload("AddPackageToVolumeAndRelatedSetAsync")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperationWithProgress<DeploymentResult,DeploymentProgress> AddPackageAsync(System.Uri packageUri, IEnumerable<System.Uri> dependencyPackageUris, DeploymentOptions options, PackageVolume targetVolume, IEnumerable<string> optionalPackageFamilyNames, IEnumerable<System.Uri> packageUrisToInstall, IEnumerable<System.Uri> relatedPackageUris);
function addPackageAsync(packageUri, dependencyPackageUris, options, targetVolume, optionalPackageFamilyNames, packageUrisToInstall, relatedPackageUris)
Public Function AddPackageAsync (packageUri As Uri, dependencyPackageUris As IEnumerable(Of Uri), options As DeploymentOptions, targetVolume As PackageVolume, optionalPackageFamilyNames As IEnumerable(Of String), packageUrisToInstall As IEnumerable(Of Uri), relatedPackageUris As IEnumerable(Of Uri)) As IAsyncOperationWithProgress(Of DeploymentResult, DeploymentProgress)

参数

packageUri
Uri Uri

要添加的包的 URI。 URI 必须遵循 file://) (文件 URI 方案,因为唯一受支持的 URI 方案是本地文件路径和本地网络路径。

dependencyPackageUris

IIterable<Uri>

IEnumerable<Uri>

要添加的框架依赖项的 URI。 如果没有依赖项包,或者如果已注册依赖项包,则此参数可以为 null。

options
DeploymentOptions

包的部署选项。

targetVolume
PackageVolume

将包添加到的卷。

optionalPackageFamilyNames

IIterable<String>

IEnumerable<String>

IIterable<Platform::String>

IIterable<winrt::hstring>

要注册main捆绑包中的包系列名称。

packageUrisToInstall

IIterable<Uri>

IEnumerable<Uri>

要随 main 应用包一起安装的可选包的 URI。

relatedPackageUris

IIterable<Uri>

IEnumerable<Uri>

要更新到与新的可选包相同的版本的相关可选包的 URI。

返回

整个部署操作过程中的 DeploymentProgress 完成百分比。

属性

Windows 要求

设备系列
Windows 10 Fall Creators Update (在 10.0.16299.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v5.0 中引入)

另请参阅

适用于