Esempio n. 1
0
        // </SnippetGeneratorIncrementalLoadingClass>

        // <SnippetFindItems>
        private async void FindItems()
        {
            this.cts = new CancellationTokenSource();

            try
            {
                this.importSession = this.importSource.CreateImportSession();

                // Progress handler for FindItemsAsync
                var progress = new Progress <uint>((result) =>
                {
                    System.Diagnostics.Debug.WriteLine(String.Format("Found {0} Files", result.ToString()));
                });

                this.itemsResult =
                    await this.importSession.FindItemsAsync(PhotoImportContentTypeFilter.ImagesAndVideos, PhotoImportItemSelectionMode.SelectAll)
                    .AsTask(this.cts.Token, progress);

                // GeneratorIncrementalLoadingClass is used to incrementally load items in the Listview view including thumbnails
                this.itemsToImport = new GeneratorIncrementalLoadingClass <ImportableItemWrapper>(this.itemsResult.TotalCount,
                                                                                                  (int index) =>
                {
                    return(new ImportableItemWrapper(this.itemsResult.FoundItems[index]));
                });

                // Set the items source for the ListView control
                this.fileListView.ItemsSource = this.itemsToImport;

                // Log the find results
                if (this.itemsResult != null)
                {
                    var findResultProperties = new System.Text.StringBuilder();
                    findResultProperties.AppendLine(String.Format("Photos\t\t\t :  {0} \t\t Selected Photos\t\t:  {1}", itemsResult.PhotosCount, itemsResult.SelectedPhotosCount));
                    findResultProperties.AppendLine(String.Format("Videos\t\t\t :  {0} \t\t Selected Videos\t\t:  {1}", itemsResult.VideosCount, itemsResult.SelectedVideosCount));
                    findResultProperties.AppendLine(String.Format("SideCars\t\t :  {0} \t\t Selected Sidecars\t:  {1}", itemsResult.SidecarsCount, itemsResult.SelectedSidecarsCount));
                    findResultProperties.AppendLine(String.Format("Siblings\t\t\t :  {0} \t\t Selected Sibilings\t:  {1} ", itemsResult.SiblingsCount, itemsResult.SelectedSiblingsCount));
                    findResultProperties.AppendLine(String.Format("Total Items Items\t :  {0} \t\t Selected TotalCount \t:  {1}", itemsResult.TotalCount, itemsResult.SelectedTotalCount));
                    System.Diagnostics.Debug.WriteLine(findResultProperties.ToString());
                }

                if (this.itemsResult.HasSucceeded)
                {
                    // Update UI to indicate success
                    System.Diagnostics.Debug.WriteLine("FindItemsAsync succeeded.");
                }
                else
                {
                    // Update UI to indicate that the operation did not complete
                    System.Diagnostics.Debug.WriteLine("FindItemsAsync did not succeed or was not completed.");
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Photo import find items operation failed. " + ex.Message);
            }


            this.cts = null;
        }
Esempio n. 2
0
        /// <summary>
        /// Enumerate all PTP / MTP sources when FindAllSources button is clicked.
        /// </summary>
        /// <param name="sender">The source of the click event</param>
        /// <param name="e">Details about the click event </param>
        private async void OnFindSourcesClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                rootPage.NotifyUser(String.Empty, NotifyType.StatusMessage);

                // clear our list of items to import
                this.itemsToImport            = null;
                this.fileListView.ItemsSource = this.itemsToImport;

                // reset UI elements
                this.progressBar.Value     = 0.0;
                this.propertiesTxtBox.Text = "";

                // disable all buttons until source selection is made
                setButtonState(false);

                // reenable source selection
                this.sourceListBox.IsEnabled = true;

                // clear find criteria
                this.OnlyImages.IsChecked     = false;
                this.OnlyVideo.IsChecked      = false;
                this.ImagesAndVideo.IsChecked = false;



                // Dispose any created import session
                if (this.session != null)
                {
                    this.session.Dispose();
                    this.session = null;
                }

                // Check to ensure API is supported
                bool importSupported = await PhotoImportManager.IsSupportedAsync();

                if (importSupported)
                {
                    // enumerate all available sources to where we can import from
                    sourceListBox.ItemsSource = await PhotoImportManager.FindAllSourcesAsync();
                }
                else
                {
                    rootPage.NotifyUser("Import is not supported.", NotifyType.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("FindAllSourcesAsync Failed. " + "Exception: " + ex.ToString(), NotifyType.ErrorMessage);
            }
        }
Esempio n. 3
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);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Helper method to Enumerate all importable items
        /// </summary>
        /// <param name="contentType">the content type</param>
        /// <param name="selectionMode">the selection mode</param>
        private async void findItems(PhotoImportContentTypeFilter contentType, PhotoImportItemSelectionMode selectionMode)
        {
            rootPage.NotifyUser(String.Empty, NotifyType.StatusMessage);

            // Disable buttons and lists to prevent unexpected reentrance.
            setButtonState(false);
            this.sourceListBox.IsEnabled = false;

            this.cts = new CancellationTokenSource();

            try
            {
                this.progressRing.IsActive    = true;
                this.itemsToImport            = null;
                this.fileListView.ItemsSource = null;

                // Dispose any created import session
                if (this.session != null)
                {
                    this.session.Dispose();
                }

                // Create the import session from the source selected
                this.session = importSource.CreateImportSession();

                RaisePropertyChanged("AppendSessionDateToDestinationFolder");

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

                // Find all items available importable item from our source
                this.itemsResult = await session.FindItemsAsync(contentType, selectionMode).AsTask(cts.Token, progress);

                setIncrementalFileList(this.itemsResult);
                DisplayFindItemsResult();

                if (this.itemsResult.HasSucceeded)
                {
                    // disable Find New , Find All and Delete button.
                    this.findNewItemsButton.IsEnabled = false;
                    this.findAllItemsButton.IsEnabled = false;
                    this.deleteButton.IsEnabled       = false;

                    // enable list selection , folder options and import buttons
                    this.selectNewButton.IsEnabled      = true;
                    this.selectNoneButton.IsEnabled     = true;
                    this.selectAllButton.IsEnabled      = true;
                    this.importButton.IsEnabled         = true;
                    this.fileListView.IsEnabled         = true;
                    this.AddSessionDateFolder.IsEnabled = true;
                    this.NoSubFolder.IsEnabled          = true;
                    this.FileDateSubfolder.IsEnabled    = true;
                    this.ExifDateSubfolder.IsEnabled    = true;
                    this.KeepOriginalFolder.IsEnabled   = true;
                }
                else
                {
                    rootPage.NotifyUser("FindItemsAsync did not succeed or was not completed.", NotifyType.StatusMessage);

                    // re-enable Find New and Find All buttons
                    this.findNewItemsButton.IsEnabled = true;
                    this.findAllItemsButton.IsEnabled = true;

                    this.progressRing.IsActive = false;
                }

                this.progressRing.IsActive = false;

                // Set the CancellationTokenSource to null when the work is complete.
                cts = null;
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("Error: Operation incomplete. " + "Exception: " + ex.ToString(), NotifyType.ErrorMessage);
            }
        }
        /// <summary>
        /// Enumerate all PTP / MTP sources when FindAllSources button is clicked. 
        /// </summary>
        /// <param name="sender">The source of the click event</param>
        /// <param name="e">Details about the click event </param>
        private async void OnFindSourcesClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                rootPage.NotifyUser(String.Empty, NotifyType.StatusMessage);

                // clear our list of items to import
                this.itemsToImport = null;
                this.fileListView.ItemsSource = this.itemsToImport;

                // reset UI elements
                this.progressBar.Value = 0.0;
                this.propertiesTxtBox.Text = "";

                // disable all buttons until source selection is made
                setButtonState(false);

                // reenable source selection
                this.sourceListBox.IsEnabled = true;

                // clear find criteria
                this.OnlyImages.IsChecked = false;
                this.OnlyVideo.IsChecked = false;
                this.ImagesAndVideo.IsChecked = false;
                


                // Dispose any created import session
                if (this.session != null)
                {
                    this.session.Dispose();
                    this.session = null;
                }

                // Check to ensure API is supported
                bool importSupported = await PhotoImportManager.IsSupportedAsync();

                if (importSupported)
                {
                    // enumerate all available sources to where we can import from
                    sourceListBox.ItemsSource = await PhotoImportManager.FindAllSourcesAsync();
                }
                else
                {
                    rootPage.NotifyUser("Import is not supported.", NotifyType.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("FindAllSourcesAsync Failed. " + "Exception: " + ex.ToString(), NotifyType.ErrorMessage);
            }
        }
        /// <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>
        /// Helper method to Enumerate all importable items
        /// </summary>
        /// <param name="contentType">the content type</param>
        /// <param name="selectionMode">the selection mode</param>
        private async void findItems(PhotoImportContentTypeFilter contentType, PhotoImportItemSelectionMode selectionMode)
        {
            rootPage.NotifyUser(String.Empty, NotifyType.StatusMessage);

            // Disable buttons and lists to prevent unexpected reentrance.
            setButtonState(false);
            this.sourceListBox.IsEnabled = false;

            this.cts = new CancellationTokenSource();

            try
            {
                this.progressRing.IsActive = true;
                this.itemsToImport = null;
                this.fileListView.ItemsSource = null;

                // Dispose any created import session
                if (this.session != null)
                {
                    this.session.Dispose();
                    this.session = null;
                }

                // Create the import session from the source selected
                this.session = importSource.CreateImportSession();

                RaisePropertyChanged("AppendSessionDateToDestinationFolder");

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

                // Find all items available importable item from our source
                this.itemsResult = await session.FindItemsAsync(contentType, selectionMode).AsTask(cts.Token, progress);

                setIncrementalFileList(this.itemsResult);
                DisplayFindItemsResult();

                if (this.itemsResult.HasSucceeded)
                {
                    // disable Find New , Find All and Delete button. 
                    this.findNewItemsButton.IsEnabled = false;
                    this.findAllItemsButton.IsEnabled = false;
                    this.deleteButton.IsEnabled = false;
                    
                    // enable list selection , folder options and import buttons
                    this.selectNewButton.IsEnabled = true;
                    this.selectNoneButton.IsEnabled = true;
                    this.selectAllButton.IsEnabled = true;
                    this.importButton.IsEnabled = true;
                    this.fileListView.IsEnabled = true;
                    this.AddSessionDateFolder.IsEnabled = true;
                    this.NoSubFolder.IsEnabled = true;
                    this.FileDateSubfolder.IsEnabled = true;
                    this.ExifDateSubfolder.IsEnabled = true;
                    this.KeepOriginalFolder.IsEnabled = true;

                }
                else
                {                    
                    rootPage.NotifyUser("FindItemsAsync did not succeed or was not completed.", NotifyType.StatusMessage);
                    
                    // re-enable Find New and Find All buttons
                    this.findNewItemsButton.IsEnabled = true;
                    this.findAllItemsButton.IsEnabled = true;

                    this.progressRing.IsActive = false;
                }

                this.progressRing.IsActive = false;
                
                // Set the CancellationTokenSource to null when the work is complete.
                cts = null;
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("Error: Operation incomplete. " + "Exception: " + ex.ToString(), NotifyType.ErrorMessage);
            }
        }