语言

PackageManager.AddPackageByAppInstallerFileAsync 方法

定义

允许使用 .appinstaller 文件安装单个或多个应用 包 。

public:
 virtual IAsyncOperationWithProgress<DeploymentResult ^, DeploymentProgress> ^ AddPackageByAppInstallerFileAsync(Uri ^ appInstallerFileUri, AddPackageByAppInstallerOptions options, PackageVolume ^ targetVolume) = AddPackageByAppInstallerFileAsync;
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> AddPackageByAppInstallerFileAsync(Uri const& appInstallerFileUri, AddPackageByAppInstallerOptions const& options, PackageVolume const& targetVolume);
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperationWithProgress<DeploymentResult,DeploymentProgress> AddPackageByAppInstallerFileAsync(System.Uri appInstallerFileUri, AddPackageByAppInstallerOptions options, PackageVolume targetVolume);
function addPackageByAppInstallerFileAsync(appInstallerFileUri, options, targetVolume)
Public Function AddPackageByAppInstallerFileAsync (appInstallerFileUri As Uri, options As AddPackageByAppInstallerOptions, targetVolume As PackageVolume) As IAsyncOperationWithProgress(Of DeploymentResult, DeploymentProgress)

参数

appInstallerFileUri
Uri Uri

.appinstaller 文件的 URI。

targetVolume
PackageVolume

包安装到的卷。

返回

具有 DeploymentResult的 IAsyncOperationWithProgress,指示部署是否成功,以及安装过程中提供进度更新的 DeploymentProgress。

属性

Windows 要求

要求 说明
设备系列
Windows 10 Fall Creators Update (在 10.0.16299.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v5.0 中引入)
应用功能
packageManagement

示例

以下示例使用 AddPackageByAppInstallerFileAsync 安装 .appinstaller 文件中定义的包。 请注意,返回的 IAsyncOperationWithProgress 是在监视变量完成之前分配给变量的。 未能保存对操作的引用可能会导致它被垃圾回收,并且永远不会完成。

using System;
using System.Threading;
using Windows.Foundation;
using Windows.Management.Deployment;

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

    Uri appInstallerFileUri = new Uri(inputFileUri);

    PackageManager packageManager = new PackageManager();
    PackageVolume packageVolume = packageManager.GetDefaultPackageVolume();

    // Important: assign the operation to a variable to prevent it from
    // being garbage-collected before completion.
    IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> deploymentOperation =
        packageManager.AddPackageByAppInstallerFileAsync(
            appInstallerFileUri,
            AddPackageByAppInstallerOptions.ForceTargetAppShutdown,
            packageVolume);

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

    // Define the delegate using a statement lambda
    deploymentOperation.Completed = (operation, 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;
}

注解

JavaScript 不支持此方法。 但是,可以创建调用此方法的Windows 运行时组件,然后从 JavaScript UWP 应用调用此组件。 有关详细信息,请参阅 应用安装程序文件 API 问题。

适用于

另请参阅