示例#1
0
        /// <summary>
        /// Invoked when all uplaod button is clicked.
        /// </summary>
        /// <param name="sender">The all upload button clicked.</param>
        /// <param name="e">Event data that describes how the click was initiated.</param>
        /// <exception cref="InvalidDataException">
        /// </exception>
        private async void allUploadButton_Click(object sender, RoutedEventArgs e)
        {
            if (!CheckCourseInfomation())
            {
                ShowMessageDialog("Format error4! Please check your upload infomation.");
                return;
            }

            addLessonButton.IsEnabled    = false;
            allUploadButton.IsEnabled    = false;
            resetUploadButton.IsEnabled  = false;
            uploadProgressBar.Visibility = Visibility.Visible;

            try
            {
                resourceDic = new Dictionary <string, int>(Constants.ResourceType.Count);
                for (int i = 0; i < Constants.ResourceType.Count; ++i)
                {
                    DataServiceQuery <RES_TYPE> dps = (DataServiceQuery <RES_TYPE>)(from res_type in ctx.RES_TYPE
                                                                                    where res_type.DESCRIPTION.Trim() == Constants.ResourceType[i]
                                                                                    select res_type);
                    TaskFactory <IEnumerable <RES_TYPE> > tf = new TaskFactory <IEnumerable <RES_TYPE> >();
                    RES_TYPE resID = (await tf.FromAsync(dps.BeginExecute(null, null), iar => dps.EndExecute(iar))).FirstOrDefault();

                    resourceDic.Add(Constants.ResourceType[i], resID.ID);
                }
            }
            catch
            {
                ShowMessageDialog("Network connection error5!");
                return;
            }

            string courseUplaodUri = "/CreateCourse?teacher_id=" + Constants.User.ID
                                     + "&title='" + courseNameTextBox.Text
                                     + "'&intro='" + CourseDescriptionTextBox.Text
                                     + "'&category_id=" + (categoryComboBox.SelectionBoxItem as CATEGORY).ID
                                     + "&price=" + Convert.ToDecimal(priceTextBox.Text)
                                     + "&pg_id=" + (pgComboBox.SelectionBoxItem as PARENT_GUIDE).ID
                                     + "&icon_url='" + toBeUploadCourse.ImageUri + "'";

            int newCourseID = 0;

            try
            {
                TaskFactory <IEnumerable <decimal> > tf = new TaskFactory <IEnumerable <decimal> >();
                IEnumerable <decimal> courses           = await tf.FromAsync(ctx.BeginExecute <decimal>(new Uri(courseUplaodUri, UriKind.Relative), null, null), iar => ctx.EndExecute <decimal>(iar));

                newCourseID = Convert.ToInt32(courses.FirstOrDefault());
                if (newCourseID == 0)
                {
                    throw new InvalidDataException();
                }
            }
            catch
            {
                ShowMessageDialog("Course upload error6! Please check your network.");
                return;
            }

            for (int i = 0; i < allLessons.Count; ++i)
            {
                Lesson newLesson       = allLessons[i];
                string lessonUploadUri = "/CreateLesson?course_id=" + newCourseID
                                         + "&content='" + newLesson.Content
                                         + "'&title='" + newLesson.Title +
                                         "'&number=" + newLesson.Number;
                int newLessonID = 0;
                try
                {
                    TaskFactory <IEnumerable <decimal> > tf = new TaskFactory <IEnumerable <decimal> >();
                    IEnumerable <decimal> lessons           = await tf.FromAsync(ctx.BeginExecute <decimal>(new Uri(lessonUploadUri, UriKind.Relative), null, null), iar => ctx.EndExecute <decimal>(iar));

                    newLessonID = Convert.ToInt32(lessons.FirstOrDefault());
                    if (newLessonID == 0)
                    {
                        throw new InvalidDataException();
                    }
                }
                catch
                {
                    ShowMessageDialog("Lesson error7! Please check your network.");
                    return;
                }

                try
                {
                    List <Resource> docsRes   = allLessons[i].GetDocList();
                    List <Resource> audiosRes = allLessons[i].GetAudioList();
                    List <Resource> videosRes = allLessons[i].GetVideoList();
                    for (int j = 0; j < docsRes.Count; ++j)
                    {
                        Resource r      = docsRes[j];
                        RESOURCE newRes = new RESOURCE();
                        newRes.LESSON_ID = newLessonID;
                        newRes.TYPE      = resourceDic["DOCUMENT"];
                        newRes.URL       = r.Uri;
                        newRes.TITLE     = r.Title;

                        ctx.AddToRESOURCE(newRes);
                    }
                    for (int j = 0; j < audiosRes.Count; ++j)
                    {
                        Resource r      = audiosRes[j];
                        RESOURCE newRes = new RESOURCE();
                        newRes.LESSON_ID = newLessonID;
                        newRes.TYPE      = resourceDic["AUDIO"];
                        newRes.URL       = r.Uri;
                        newRes.TITLE     = r.Title;

                        ctx.AddToRESOURCE(newRes);
                    }
                    for (int j = 0; j < videosRes.Count; ++j)
                    {
                        Resource r      = videosRes[j];
                        RESOURCE newRes = new RESOURCE();
                        newRes.LESSON_ID = newLessonID;
                        newRes.TYPE      = resourceDic["VIDEO"];
                        newRes.URL       = r.Uri;
                        newRes.TITLE     = r.Title;

                        ctx.AddToRESOURCE(newRes);
                    }
                    TaskFactory <DataServiceResponse> tfRes = new TaskFactory <DataServiceResponse>();
                    DataServiceResponse dsr = await tfRes.FromAsync(ctx.BeginSaveChanges(null, null), iar => ctx.EndSaveChanges(iar));
                }
                catch
                {
                    ShowMessageDialog("Uplaod error8! Please check your network");
                    return;
                }
            }
            addLessonButton.IsEnabled    = true;
            allUploadButton.IsEnabled    = true;
            resetUploadButton.IsEnabled  = true;
            uploadProgressBar.Visibility = Visibility.Collapsed;

            var finishMsg = new MessageDialog("Upload Finish! Please wait the check.", "Congratulation");

            finishMsg.Commands.Add(new UICommand("Continue upload", (command) =>
            {
                ResetPage();
            }));
            finishMsg.Commands.Add(new UICommand("Back", (command) =>
            {
                Frame.Navigate(typeof(CourseStore.Courstore));
            }));
            await finishMsg.ShowAsync();
        }