示例#1
0
        /// <summary>
        /// Gets an object from the user's OneDrive directory
        /// </summary>
        /// <param name="path">The path to the object</param>
        /// <returns></returns>
        public static async Task<OneDriveObject> GetObject(string path)
        {
            try
            {
                //Create a new One Drive Object
                OneDriveObject curObject = new OneDriveObject();

                LiveConnectClient connect = new LiveConnectClient(Session.CurSession);
                LiveOperationResult operationResult = await connect.GetAsync(path);
                dynamic result = operationResult.Result;
                if (result != null)
                {
                    //Create a new One Drive object from the results
                    curObject = new OneDriveObject(result.name, result.description, result.id, result.parent_id,
                                                    result.type, result.size, result.created_time, result.updated_time);
                }

                //Return our object
                return curObject;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }
        /// <summary>
        /// Clear the selection for both the myfiles and shared files lists
        /// </summary>
        private void ClearSelection()
        {
            //Clear myFiles list
            if (lstMyFiles.ItemsSource != null)
            {
                foreach (OneDriveObject item in lstMyFiles.ItemsSource)
                {
                    item.TextColor = new SolidColorBrush(Colors.White);
                }
            }

            //Clear sharedFiles list
            if (lstSharedFiles.ItemsSource != null)
            {
                foreach (OneDriveObject item in lstSharedFiles.ItemsSource)
                {
                    item.TextColor = new SolidColorBrush(Colors.White);
                }
            }

            //Clear list selections
            lstSharedFiles.SelectedItem = null;
            lstMyFiles.SelectedItem = null;

            //Clear "current" variables
            myCurFile = null;
            myCurListItem = null;
            sharedCurFile = null;
            sharedCurListItem = null;
            Session.SharedFile = null;
        }
        /// <summary>
        /// Handle the back key navigation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                //Stop playing the preview
                StopMedia();

                //Disable the play, share, and save buttons
                appBarMediaButton.IsEnabled = false;
                appBarSaveButton.IsEnabled = false;
                appBarShareButton.IsEnabled = false;

                //Clear curfile
                myCurFile = null;
                sharedCurFile = null;

                //Determine what list the user is currently viewing
                switch (ringtonePanorama.SelectedIndex)
                {
                    case MyFiles:
                        if (myCurDirectory != null && myCurDirectory.ParentID != null)
                        {
                            //Cancel navigating away from this page
                            e.Cancel = true;

                            //Load the previous directory
                            myFilesList = await FetchDirectory(string.Concat(myCurDirectory.ParentID, "/files"));
                            lstMyFiles.ItemsSource = myFilesList;
                            //Set the current directory
                            myCurDirectory = await OneDriveObject.GetObject(myCurDirectory.ParentID);
                        }
                        break;
                    case SharedFiles:
                        if (sharedCurDirectory != null && sharedCurDirectory.ParentID != null)
                        {
                            //Cancel navigating away from this page
                            e.Cancel = true;

                            //Load the previous directory
                            sharedFilesList = await FetchDirectory(string.Concat(sharedCurDirectory.ParentID, "/files"));
                            lstSharedFiles.ItemsSource = sharedFilesList;
                            //Set the current directory
                            sharedCurDirectory = await OneDriveObject.GetObject(sharedCurDirectory.ParentID);
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// Highlight the selected item in the list
        /// </summary>
        /// <param name="selectedItem"></param>
        /// <param name="itemList"></param>
        private void HighlightSelection(OneDriveObject selectedItem, ObservableCollection<OneDriveObject> itemList)
        {
            //Clear existing selection
            foreach (OneDriveObject item in itemList)
            {
                item.TextColor = new SolidColorBrush(Colors.White);
            }

            //Highlight selected item
            selectedItem.TextColor = new SolidColorBrush(Session.AccentColor);
        }
        /// <summary>
        /// The user selected an item in the OneDrive list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void lstSharedDrive_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //Exit if the selected item is null
            if (lstSharedFiles.SelectedItem == null) return;

            //If the selected item is the same as the current item, exit
            if (lstSharedFiles.SelectedItem == sharedCurListItem) return;

            //Clear the my files list selection
            ClearSelectionForList(myFilesList, lstMyFiles);
            //Get a referenc to the current list item
            sharedCurListItem = lstSharedFiles.SelectedItem;

            //Highlight the selected item
            HighlightSelection((OneDriveObject)sharedCurListItem, sharedFilesList);

            //Stop playing the preview
            StopMedia();

            //Disable the play, save, and share buttons
            DisableButtons();

            //Get the one drive object
            OneDriveObject selectedItem = (OneDriveObject)sharedCurListItem;

            //Take action based on the type of item selected
            switch (selectedItem.ObjectType)
            {
                case "folder":
                    //Navigate to the folder
                    sharedFilesList = await FetchDirectory(string.Concat(selectedItem.ID, "/files"));
                    lstSharedFiles.ItemsSource = sharedFilesList;
                    //Set the current object
                    sharedCurDirectory = await OneDriveObject.GetObject(selectedItem.ID);
                    break;
                case "audio":
                    //Set the current object
                    sharedCurFile = await OneDriveObject.GetObject(selectedItem.ID);
                    DownloadFile(selectedItem.ID, selectedItem.Name);
                    break;
            }
        }
        /// <summary>
        /// Download the selected file to local storage
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileName"></param>
        private async void DownloadFile(string path, string fileName)
        {
            try
            {
                //Get a reference to the list item
                OneDriveObject lstItem = new OneDriveObject();

                switch(ringtonePanorama.SelectedIndex)
                {
                    case MyFiles:
                        lstItem = (OneDriveObject)lstMyFiles.SelectedItem;
                        break;
                    case SharedFiles:
                        lstItem = (OneDriveObject)lstSharedFiles.SelectedItem;
                        break;
                }
                
                Progress<LiveOperationProgress> downloadProgress = new Progress<LiveOperationProgress>(
                    (p) =>
                    {
                        if (lstItem != null)
                        {
                            lstItem.DownloadProgress = p.ProgressPercentage;
                        }
                    });

                //Fetch the file from the user's One Drive account
                LiveConnectClient downloadClient = new LiveConnectClient(Session.CurSession);
                LiveDownloadOperationResult downloadResult = await downloadClient.DownloadAsync(
                        string.Concat(path, "/content"), CancellationToken.None, downloadProgress );
                
                //Move the file to the apps local storage
                Stream fileStream = downloadResult.Stream;
                using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream fileToSave = storage.OpenFile(fileName, FileMode.Create, FileAccess.ReadWrite))
                    {
                        fileStream.CopyTo(fileToSave);
                        fileStream.Flush();
                        fileStream.Close();                      
                    }                   

                    using (Stream isoStream = storage.OpenFile(fileName, FileMode.Open))
                    {
                        mediaPlayer.SetSource(isoStream);
                    }
                }

                //Clear the download progress
                lstItem.DownloadProgress = 0;

                //Enable the save and share buttons
                appBarSaveButton.IsEnabled = true;
                appBarShareButton.IsEnabled = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }