示例#1
0
        static async Task <bool> AddHostedPackageAsync(string pathToPackage, bool isPackageFile)
        {
            var packageUri = new Uri(pathToPackage);

            Console.WriteLine(" Package Address {0}", pathToPackage);
            Console.WriteLine(" Package Uri {0}", packageUri);

            PackageManager packageManager = new PackageManager();

            Task <DeploymentResult> deployment = null;

            // Set AllowUnsigned=true for unsigned Hosted Apps
            if (isPackageFile)
            {
                var addOptions = new AddPackageOptions {
                    AllowUnsigned = true
                };
                deployment = packageManager.AddPackageByUriAsync(packageUri, addOptions).AsTask();
            }
            else
            {
                var regOptions = new RegisterPackageOptions {
                    AllowUnsigned = true
                };
                deployment = packageManager.RegisterPackageByUriAsync(packageUri, regOptions).AsTask();
            }

            Console.WriteLine("Installing package {0}", packageUri);
            Debug.WriteLine("Waiting for package registration to complete...");

            try {
                await deployment;
                Console.WriteLine("Registration succeeded! Try running the new app.");
                return(true);
            } catch (OperationCanceledException) {
                Console.WriteLine("Registration was canceled");
            } catch (AggregateException ex) {
                Console.WriteLine($"Installation Error: {ex}");
            }
            return(false);
        }
示例#2
0
        private static bool registerSparsePackage(string externalLocation, string sparsePkgPath)
        {
            bool registration = false;

            try
            {
                Uri externalUri = new Uri(externalLocation);
                Uri packageUri  = new Uri(sparsePkgPath);

                Console.WriteLine("exe Location {0}", externalLocation);
                Console.WriteLine("msix Address {0}", sparsePkgPath);

                Console.WriteLine("  exe Uri {0}", externalUri);
                Console.WriteLine("  msix Uri {0}", packageUri);

                PackageManager packageManager = new PackageManager();

                //Declare use of an external location
                var options = new AddPackageOptions();
                options.ExternalLocationUri = externalUri;

                Windows.Foundation.IAsyncOperationWithProgress <DeploymentResult, DeploymentProgress> deploymentOperation = packageManager.AddPackageByUriAsync(packageUri, options);

                ManualResetEvent opCompletedEvent = new ManualResetEvent(false); // this event will be signaled when the deployment operation has completed.

                deploymentOperation.Completed = (depProgress, status) => { opCompletedEvent.Set(); };

                Console.WriteLine("Installing package {0}", sparsePkgPath);

                Debug.WriteLine("Waiting for package registration to complete...");

                opCompletedEvent.WaitOne();

                if (deploymentOperation.Status == Windows.Foundation.AsyncStatus.Error)
                {
                    Windows.Management.Deployment.DeploymentResult deploymentResult = deploymentOperation.GetResults();
                    Debug.WriteLine("Installation Error: {0}", deploymentOperation.ErrorCode);
                    Debug.WriteLine("Detailed Error Text: {0}", deploymentResult.ErrorText);
                }
                else if (deploymentOperation.Status == Windows.Foundation.AsyncStatus.Canceled)
                {
                    Debug.WriteLine("Package Registration Canceled");
                }
                else if (deploymentOperation.Status == Windows.Foundation.AsyncStatus.Completed)
                {
                    registration = true;
                    Debug.WriteLine("Package Registration succeeded!");
                }
                else
                {
                    Debug.WriteLine("Installation status unknown");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("AddPackageSample failed, error message: {0}", ex.Message);
                Console.WriteLine("Full Stacktrace: {0}", ex.ToString());

                return(registration);
            }

            return(registration);
        }
示例#3
0
        public static async Task <bool> InstallPackage(PackageInstance package, ProductDetails product, bool?useAppInstaller = null)
        {
            ToastNotification finalNotif = GenerateInstallSuccessToast(package, product);
            bool isSuccess = true;

            try
            {
                (await DownloadPackage(package, product, false)).Deconstruct(out var installer, out var progressToast);

                PackageManager pkgManager = new PackageManager();
                Progress <DeploymentProgress> progressCallback = new Progress <DeploymentProgress>(prog =>
                {
                    ToastNotificationManager.GetDefault().CreateToastNotifier().Update(
                        new NotificationData(new Dictionary <string, string>()
                    {
                        { "progressValue", (prog.percentage / 100).ToString() },
                        { "progressStatus", "Installing..." }
                    }),
                        progressToast.Tag
                        );
                });

                if (Settings.Default.UseAppInstaller || (useAppInstaller.HasValue && useAppInstaller.Value))
                {
                    // Pass the file to App Installer to install it
                    Uri launchUri = new Uri("ms-appinstaller:?source=" + installer.Path);
                    switch (await Launcher.QueryUriSupportAsync(launchUri, LaunchQuerySupportType.Uri))
                    {
                    case LaunchQuerySupportStatus.Available:
                        isSuccess = await Launcher.LaunchUriAsync(launchUri);

                        if (!isSuccess)
                        {
                            finalNotif = GenerateInstallFailureToast(package, product, new Exception("Failed to launch App Installer."));
                        }
                        break;

                    case LaunchQuerySupportStatus.AppNotInstalled:
                        finalNotif = GenerateInstallFailureToast(package, product, new Exception("App Installer is not available on this device."));
                        isSuccess  = false;
                        break;

                    case LaunchQuerySupportStatus.AppUnavailable:
                        finalNotif = GenerateInstallFailureToast(package, product, new Exception("App Installer is not available right now, try again later."));
                        isSuccess  = false;
                        break;

                    case LaunchQuerySupportStatus.Unknown:
                    default:
                        finalNotif = GenerateInstallFailureToast(package, product, new Exception("An unknown error occured."));
                        isSuccess  = false;
                        break;
                    }
                }
                else
                {
                    // Attempt to install the downloaded package
                    var result = await pkgManager.AddPackageByUriAsync(
                        new Uri(installer.Path),
                        new AddPackageOptions()
                    {
                        ForceAppShutdown = true
                    }
                        ).AsTask(progressCallback);

                    if (result.IsRegistered)
                    {
                        finalNotif = GenerateInstallSuccessToast(package, product);
                    }
                    else
                    {
                        finalNotif = GenerateInstallFailureToast(package, product, result.ExtendedErrorCode);
                    }
                    isSuccess = result.IsRegistered;
                    await installer.DeleteAsync();
                }

                // Hide progress notification
                ToastNotificationManager.GetDefault().CreateToastNotifier().Hide(progressToast);
                // Show the final notification
                ToastNotificationManager.GetDefault().CreateToastNotifier().Show(finalNotif);

                return(true);
            }
            catch (Exception ex)
            {
                ToastNotificationManager.GetDefault().CreateToastNotifier().Show(GenerateInstallFailureToast(package, product, ex));
                return(false);
            }
        }