共用方式為


PackageManager.AddPackageAsync 方法

定義

多載

AddPackageAsync(Uri, IIterable<Uri>, DeploymentOptions)

使用指定的部署選項,將 套件 (主要套件) 及其相依性套件。

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

使用指定的部署選項,將 套件 (主要套件) 及其相依性套件新增至目前使用者的指定磁片區。

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

使用指定的部署選項,將 套件 及其相依性套件新增至目前使用者的指定磁片區。

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

使用指定的部署選項,將 套件 (主要套件) 及其相依性套件。

AddPackageAsync(Uri, IIterable<Uri>, DeploymentOptions)

使用指定的部署選項,將 套件 (主要套件) 及其相依性套件。

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 必須遵循檔案 URI 配置 (file://) ,因為唯一支援的 URI 配置是本機檔案路徑和局域網路路徑。

dependencyPackageUris

IIterable<Uri>

IEnumerable<Uri>

要加入之相依性套件的 URI。 如果沒有相依性套件,或已經註冊相依性套件,此參數可以是 Null。

deploymentOptions
DeploymentOptions

來自 DeploymentOptions列舉值的位元組合。 ForceApplicationShutdownNone 是唯一有效的這個方法選項。 指定任何其他選項會導致E_INVALIDARG傳回值。

傳回

部署要求的狀態。 DeploymentResult包含部署作業的最終傳回值,完成之後。 DeploymentProgress可用來取得整個部署作業過程中完成的百分比。

屬性

Windows 需求

應用程式功能
packageManagement

範例

下列範例會使用 PackageManager.AddPackageAsync 方法來安裝沒有相依性或已安裝相依性的套件。 請注意,主要封裝的路徑會在範例中當做引數傳遞。 AddPackageAsync 會傳回可用來管理非同步作業的物件。 此範例會使用 Completed 屬性來設定 委派 ,並檢查 Status 屬性來判斷部署作業的狀態。 如果狀態為 Error,此範例會呼叫 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;
}

另請參閱 C++/WinRT 的 Visual Studio 支援

// 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)

使用指定的部署選項,將 套件 (主要套件) 及其相依性套件新增至目前使用者的指定磁片區。

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 必須遵循檔案 URI 配置 (file://) ,因為唯一支援的 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 必須遵循檔案 URI 配置 (file://) ,因為唯一支援的 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>

要註冊之主要套件組合中的套件系列名稱。

externalPackageUris

IIterable<Uri>

IEnumerable<Uri>

要註冊之主要套件組合中其他套件的 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>)

使用指定的部署選項,將 套件 (主要套件) 及其相依性套件。

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 必須遵循檔案 URI 配置 (file://) ,因為唯一支援的 URI 配置是本機檔案路徑和局域網路路徑。

dependencyPackageUris

IIterable<Uri>

IEnumerable<Uri>

要加入之架構相依性的 URI。 如果沒有相依性套件,或已經註冊相依性套件,此參數可以是 Null。

options
DeploymentOptions

封裝的部署選項。

targetVolume
PackageVolume

將封裝新增至其中的磁片區。

optionalPackageFamilyNames

IIterable<String>

IEnumerable<String>

IIterable<Platform::String>

IIterable<winrt::hstring>

要註冊之主要套件組合中的套件系列名稱。

packageUrisToInstall

IIterable<Uri>

IEnumerable<Uri>

要與主要應用程式套件一起安裝的選擇性套件 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 引進)

另請參閱

適用於