Exemple #1
0
        // </SnippetDeclareImportResult>

        // <SnippetImportClick>
        private async void importButton_Click(object sender, RoutedEventArgs e)
        {
            cts = new CancellationTokenSource();
            progressBar.Value = 0;

            try
            {
                if (itemsResult.SelectedTotalCount <= 0)
                {
                    System.Diagnostics.Debug.WriteLine("Nothing Selected for Import.");
                }
                else
                {
                    var progress = new Progress <PhotoImportProgress>((result) =>
                    {
                        progressBar.Value = result.ImportProgress;
                    });

                    this.itemsResult.ItemImported += async(s, a) =>
                    {
                        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            System.Diagnostics.Debug.WriteLine(String.Format("Imported: {0}", a.ImportedItem.Name));
                        });
                    };

                    // import items from the our list of selected items
                    this.importedResult = await this.itemsResult.ImportItemsAsync().AsTask(cts.Token, progress);

                    if (importedResult != null)
                    {
                        StringBuilder importedSummary = new StringBuilder();
                        importedSummary.AppendLine(String.Format("Photos Imported   \t:  {0} ", importedResult.PhotosCount));
                        importedSummary.AppendLine(String.Format("Videos Imported    \t:  {0} ", importedResult.VideosCount));
                        importedSummary.AppendLine(String.Format("SideCars Imported   \t:  {0} ", importedResult.SidecarsCount));
                        importedSummary.AppendLine(String.Format("Siblings Imported   \t:  {0} ", importedResult.SiblingsCount));
                        importedSummary.AppendLine(String.Format("Total Items Imported \t:  {0} ", importedResult.TotalCount));
                        importedSummary.AppendLine(String.Format("Total Bytes Imported \t:  {0} ", importedResult.TotalSizeInBytes));

                        System.Diagnostics.Debug.WriteLine(importedSummary.ToString());
                    }

                    if (!this.importedResult.HasSucceeded)
                    {
                        System.Diagnostics.Debug.WriteLine("ImportItemsAsync did not succeed or was not completed");
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Files could not be imported. " + "Exception: " + ex.ToString());
            }

            cts = null;
        }
Exemple #2
0
        /// <summary
        /// This app is registered as a hanlder for WPD\IMageSourceAutoPlay event.
        /// When a user connects a WPD device (ie: Camera), OnNavigatedTo is called with DeviceInformationId
        /// that we can call FromIdAsync() and preselect the device.
        ///
        /// We also need to handle the situation where the client was terminated while an import operation was in flight.
        /// Import will continue and we attempt to reconnect back to the session.
        /// </summary>
        /// <param name="e">Details about the NavigationEventArgs</param>
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            rootPage = MainPage.Current;
            try
            {
                //An photo import session was in progress
                if (e.Parameter is PhotoImportOperation)
                {
                    PhotoImportOperation op = e.Parameter as PhotoImportOperation;
                    switch (op.Stage)
                    {
                    // file enumeration was running
                    case PhotoImportStage.FindingItems:

                        cts = new CancellationTokenSource();
                        rootPage.NotifyUser("Reconnected back to the file enumeration stage", NotifyType.StatusMessage);

                        // Set up progress handler for existing file enumeration
                        var findAllItemsProgress = new Progress <uint>((result) =>
                        {
                            rootPage.NotifyUser(String.Format("Found {0} Files", result.ToString()), NotifyType.StatusMessage);
                        });

                        // retrieve previous session
                        this.session = op.Session;
                        // get to the operation for the existing FindItemsAsync session
                        this.itemsResult = await op.ContinueFindingItemsAsync.AsTask(cts.Token, findAllItemsProgress);

                        // display the items we found in the previous session
                        setIncrementalFileList(this.itemsResult);
                        this.selectAllButton.IsEnabled  = true;
                        this.selectNoneButton.IsEnabled = true;
                        this.selectNewButton.IsEnabled  = true;
                        this.importButton.IsEnabled     = true;

                        DisplayFindItemsResult();

                        cts = null;
                        break;

                    // Import was running
                    case PhotoImportStage.ImportingItems:

                        rootPage.NotifyUser("Reconnected back to the importing stage.", NotifyType.StatusMessage);

                        setButtonState(false);
                        cts = new CancellationTokenSource();

                        // Set up progress handler for the existing import session

                        var importSelectedItemsProgress = new Progress <PhotoImportProgress>((result) =>
                        {
                            progressBar.Value = result.ImportProgress;
                        });

                        // retrieve the previous operation
                        this.session = op.Session;

                        // get the itemsResult that were found in the previous session
                        this.itemsResult = await op.ContinueFindingItemsAsync.AsTask();

                        // hook up the ItemImported Handler to show progress
                        this.itemsResult.ItemImported += async(s, a) =>
                        {
                            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                            {
                                rootPage.NotifyUser(String.Format("Imported: {0}", a.ImportedItem.Name), NotifyType.StatusMessage);
                            });
                        };

                        // Get the operation for the import operation that was in flight
                        this.importedResult = await op.ContinueImportingItemsAsync.AsTask(cts.Token, importSelectedItemsProgress);

                        DisplayImportedSummary();

                        this.findSourceButton.IsEnabled = true;
                        this.deleteButton.IsEnabled     = true;

                        cts = null;
                        break;

                    // Delete operation is in progress
                    case PhotoImportStage.DeletingImportedItemsFromSource:

                        rootPage.NotifyUser("Reconnected to the deletion stage.", NotifyType.StatusMessage);

                        this.findSourceButton.IsEnabled = false;
                        this.deleteButton.IsEnabled     = false;
                        cts = new CancellationTokenSource();

                        var progress = new Progress <double>((result) =>
                        {
                            this.progressBar.Value = result;
                        });

                        // get the operation for the existing delete session
                        this.deleteResult = await op.ContinueDeletingImportedItemsFromSourceAsync.AsTask(cts.Token, progress);

                        DisplayDeletedResults();

                        if (!deleteResult.HasSucceeded)
                        {
                            rootPage.NotifyUser("Deletion did not succeeded or was cancelled.", NotifyType.StatusMessage);
                        }

                        this.findSourceButton.IsEnabled = true;

                        // Set the CancellationTokenSource to null when the work is complete.
                        cts = null;
                        break;

                    // Idle State.
                    case PhotoImportStage.NotStarted:

                        rootPage.NotifyUser("No import tasks was started.", NotifyType.StatusMessage);
                        this.session = op.Session;
                        break;

                    default:
                        break;
                    }
                }

                // Activated by AutoPlay
                if (e.Parameter is DeviceActivatedEventArgs)
                {
                    this.arguments = e.Parameter as DeviceActivatedEventArgs;
                    bool importSupported = await PhotoImportManager.IsSupportedAsync();

                    this.sourceListBox.Items.Clear();

                    List <PhotoImportSource> source = new List <PhotoImportSource>();

                    // Create the source from a device Id
                    source.Add(await PhotoImportSource.FromIdAsync(this.arguments.DeviceInformationId));

                    // bind and preselects source
                    this.sourceListBox.ItemsSource   = source;
                    this.sourceListBox.SelectedIndex = 0;

                    rootPage.NotifyUser("Device selected from AutoPlay", NotifyType.StatusMessage);
                }
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("Exception: " + ex.ToString(), NotifyType.ErrorMessage);
            }
        }
Exemple #3
0
        /// <summary>
        /// Initiate the import operation when the Import button is clicked.
        /// </summary>
        /// <param name="sender">Source of the click event</param>
        /// <param name="e">Details about the click event</param>
        private async void OnImportButtonClicked(object sender, RoutedEventArgs e)
        {
            rootPage.NotifyUser(String.Empty, NotifyType.StatusMessage);

            // disable UI while async operations are running.
            setButtonState(false);
            this.sourceListBox.IsEnabled = false;
            this.progressBar.Value       = 0.0;

            cts = new CancellationTokenSource();

            try
            {
                if (itemsResult.SelectedTotalCount <= 0)
                {
                    rootPage.NotifyUser("Nothing Selected for Import.", NotifyType.StatusMessage);
                }
                else
                {
                    var progress = new Progress <PhotoImportProgress>((result) =>
                    {
                        progressBar.Value = result.ImportProgress;
                    });

                    this.itemsResult.ItemImported += async(s, a) =>
                    {
                        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            rootPage.NotifyUser(String.Format("Imported: {0}", a.ImportedItem.Name), NotifyType.StatusMessage);
                        });
                    };

                    // import items from the our list of selected items
                    this.importedResult = await this.itemsResult.ImportItemsAsync().AsTask(cts.Token, progress);

                    DisplayImportedSummary();

                    if (!this.importedResult.HasSucceeded)
                    {
                        rootPage.NotifyUser("ImportItemsAsync did not succeed or was not completed", NotifyType.StatusMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("Files could not be imported. " + "Exception: " + ex.ToString(), NotifyType.ErrorMessage);
            }

            cts = null;

            // allow the user to find sources and delete the files from the media
            this.findSourceButton.IsEnabled = true;
            this.deleteButton.IsEnabled     = true;

            this.importButton.IsEnabled       = false;
            this.findNewItemsButton.IsEnabled = false;
            this.findAllItemsButton.IsEnabled = false;
            this.selectNewButton.IsEnabled    = false;
            this.selectAllButton.IsEnabled    = false;
            this.selectNoneButton.IsEnabled   = false;

            this.NoSubFolder.IsChecked        = false;
            this.FileDateSubfolder.IsChecked  = false;
            this.ExifDateSubfolder.IsChecked  = false;
            this.KeepOriginalFolder.IsChecked = false;
        }
        /// <summary
        /// This app is registered as a hanlder for WPD\IMageSourceAutoPlay event.
        /// When a user connects a WPD device (ie: Camera), OnNavigatedTo is called with DeviceInformationId
        /// that we can call FromIdAsync() and preselect the device.
        /// 
        /// We also need to handle the situation where the client was terminated while an import operation was in flight.
        /// Import will continue and we attempt to reconnect back to the session.
        /// </summary>
        /// <param name="e">Details about the NavigationEventArgs</param>
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            rootPage = MainPage.Current;
            try
            {
                //An photo import session was in progress
                if (e.Parameter is PhotoImportOperation)
                {
                    PhotoImportOperation op = e.Parameter as PhotoImportOperation;
                    switch (op.Stage)
                    {
                        // file enumeration was running
                        case PhotoImportStage.FindingItems:

                            cts = new CancellationTokenSource();
                            rootPage.NotifyUser("Reconnected back to the file enumeration stage", NotifyType.StatusMessage);

                            // Set up progress handler for existing file enumeration
                            var findAllItemsProgress = new Progress<uint>((result) =>
                            {
                                rootPage.NotifyUser(String.Format("Found {0} Files", result.ToString()), NotifyType.StatusMessage);
                            });

                            // retrieve previous session
                            this.session = op.Session;
                            // get to the operation for the existing FindItemsAsync session
                            this.itemsResult = await op.ContinueFindingItemsAsync.AsTask(cts.Token, findAllItemsProgress);

                            // display the items we found in the previous session
                            setIncrementalFileList(this.itemsResult);
                            this.selectAllButton.IsEnabled = true;
                            this.selectNoneButton.IsEnabled = true;
                            this.selectNewButton.IsEnabled = true;
                            this.importButton.IsEnabled = true;

                            DisplayFindItemsResult();

                            cts = null;
                            break;

                        // Import was running
                        case PhotoImportStage.ImportingItems:

                            rootPage.NotifyUser("Reconnected back to the importing stage.", NotifyType.StatusMessage);

                            setButtonState(false);
                            cts = new CancellationTokenSource();

                            // Set up progress handler for the existing import session

                            var importSelectedItemsProgress = new Progress<PhotoImportProgress>((result) =>
                            {
                                progressBar.Value = result.ImportProgress;
                            });

                            // retrieve the previous operation
                            this.session = op.Session;
                            
                            // get the itemsResult that were found in the previous session
                            this.itemsResult = await op.ContinueFindingItemsAsync.AsTask();
                            
                            // hook up the ItemImported Handler to show progress
                            this.itemsResult.ItemImported += async (s, a) =>
                            {
                                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                                {
                                    rootPage.NotifyUser(String.Format("Imported: {0}", a.ImportedItem.Name), NotifyType.StatusMessage);
                                });
                            };

                            // Get the operation for the import operation that was in flight
                            this.importedResult = await op.ContinueImportingItemsAsync.AsTask(cts.Token, importSelectedItemsProgress);

                            DisplayImportedSummary();

                            this.findSourceButton.IsEnabled = true;
                            this.deleteButton.IsEnabled = true;

                            cts = null;
                            break;

                        // Delete operation is in progress
                        case PhotoImportStage.DeletingImportedItemsFromSource:

                            rootPage.NotifyUser("Reconnected to the deletion stage.",NotifyType.StatusMessage);

                            this.findSourceButton.IsEnabled = false;
                            this.deleteButton.IsEnabled = false;
                            cts = new CancellationTokenSource();
                                                       
                            var progress = new Progress<double>((result) =>
                            {
                                this.progressBar.Value = result;
                            });

                            // get the operation for the existing delete session
                            this.deleteResult = await op.ContinueDeletingImportedItemsFromSourceAsync.AsTask(cts.Token, progress);

                            DisplayDeletedResults();

                            if (!deleteResult.HasSucceeded)
                            {
                                rootPage.NotifyUser("Deletion did not succeeded or was cancelled.", NotifyType.StatusMessage);
                            }

                            this.findSourceButton.IsEnabled = true;

                            // Set the CancellationTokenSource to null when the work is complete.
                            cts = null;
                            break;

                        // Idle State.
                        case PhotoImportStage.NotStarted:

                            rootPage.NotifyUser("No import tasks was started.", NotifyType.StatusMessage);
                            this.session = op.Session;
                            break;

                        default:
                            break;
                    }
                }

                // Activated by AutoPlay
                if (e.Parameter is DeviceActivatedEventArgs)
                {
                    this.arguments = e.Parameter as DeviceActivatedEventArgs;
                    bool importSupported = await PhotoImportManager.IsSupportedAsync();
                    this.sourceListBox.Items.Clear();

                    List<PhotoImportSource> source = new List<PhotoImportSource>();
                    
                    // Create the source from a device Id
                    source.Add(await PhotoImportSource.FromIdAsync(this.arguments.DeviceInformationId));

                    // bind and preselects source
                    this.sourceListBox.ItemsSource = source;
                    this.sourceListBox.SelectedIndex = 0;

                    rootPage.NotifyUser("Device selected from AutoPlay", NotifyType.StatusMessage);
                }
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("Exception: " + ex.ToString(), NotifyType.ErrorMessage);
            }
        }
        /// <summary>
        /// Initiate the import operation when the Import button is clicked.
        /// </summary>
        /// <param name="sender">Source of the click event</param>
        /// <param name="e">Details about the click event</param>
        private async void OnImportButtonClicked(object sender, RoutedEventArgs e)
        {
            rootPage.NotifyUser(String.Empty, NotifyType.StatusMessage);

            // disable UI while async operations are running.
            setButtonState(false);
            this.sourceListBox.IsEnabled = false;
            this.progressBar.Value = 0.0;

            cts = new CancellationTokenSource();

            try
            {
                if (itemsResult.SelectedTotalCount <= 0)
                {
                    rootPage.NotifyUser("Nothing Selected for Import.", NotifyType.StatusMessage);
                }
                else
                {                    
                    var progress = new Progress<PhotoImportProgress>((result) =>
                    {
                        progressBar.Value =  result.ImportProgress;
                    });         
                               
                    this.itemsResult.ItemImported += async (s, a) =>
                    {                        
                        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            rootPage.NotifyUser(String.Format("Imported: {0}", a.ImportedItem.Name), NotifyType.StatusMessage);
                        });
                    };

                    // import items from the our list of selected items
                    this.importedResult = await this.itemsResult.ImportItemsAsync().AsTask(cts.Token, progress);

                    DisplayImportedSummary();

                    if (!this.importedResult.HasSucceeded)
                    {
                        rootPage.NotifyUser("ImportItemsAsync did not succeed or was not completed", NotifyType.StatusMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("Files could not be imported. " + "Exception: " + ex.ToString(), NotifyType.ErrorMessage);
            }

            cts = null;

            // allow the user to find sources and delete the files from the media
            this.findSourceButton.IsEnabled = true;
            this.deleteButton.IsEnabled = true;

            this.importButton.IsEnabled = false;
            this.findNewItemsButton.IsEnabled = false;
            this.findAllItemsButton.IsEnabled = false;
            this.selectNewButton.IsEnabled = false;
            this.selectAllButton.IsEnabled = false;
            this.selectNoneButton.IsEnabled = false;

            this.NoSubFolder.IsChecked = false;
            this.FileDateSubfolder.IsChecked = false;
            this.ExifDateSubfolder.IsChecked = false;
            this.KeepOriginalFolder.IsChecked = false;
        }