語言

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

一個 Uri 連接到 .appinstaller 檔案。

options
AddPackageByAppInstallerOptions

這些選項依 AddPackageByAppInstallerOptions 所指定的。

targetVolume
PackageVolume

套件安裝到的磁碟區。

傳回

一個包含 DeploymentResult 的 IAsyncOperationWithProgress,表示部署是否成功,以及 DeploymentProgress,提供安裝過程中的進度更新。

屬性

Windows 需求

規格需求 Description
裝置系列
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 應用程式呼叫這個元件。 更多資訊請參閱 App Installer 檔案 API 問題。

適用於

另請參閱