Exemplo n.º 1
0
        /// <summary>
        /// Thread body
        /// </summary>
        /// <param name="state"></param>
        private void StartWork_SaveAMedia(object state)
        {
            ShopproHelper.ShowBusyIndicator(this, "Saving this new media to libary...");

            MediaView mediaView  = state as MediaView;
            media     savedMedia = null;
            media     theMedia   = new media();

            mediaView.Merge(theMedia, DataFormMode.AddNew);

            // Get file content
            byte[] content = ShopproHelper.ReadFully(fileInfo.OpenRead());

            // Save to the backend
            EventHandler <createNew_MediaCompletedEventArgs> h1 = (s, e) =>
            {
                // TODO: handle error from server side
                savedMedia = e.Result;
                nextOneAutoResetEvent.Set();
            };

            Globals.WSClient.createNew_MediaCompleted += h1;
            Globals.WSClient.createNew_MediaAsync(theMedia, content);
            nextOneAutoResetEvent.WaitOne();
            Globals.WSClient.createNew_MediaCompleted -= h1;

            // Check return result. If failure, savedCategory will be null
            if (savedMedia != null)
            {
                this.ResultingImageUrl = savedMedia.myUrl;

                this.Dispatcher.BeginInvoke(delegate()
                {
                    mediaView.Restore(new MediaView(savedMedia));
                });
            }
            else
            {
                // Show error message
                ShopproHelper.ShowMessageWindow(this, "Error", "Fail to save this entry.", false);
            }

            this.fileInfo = null;

            // Hide busy indicator
            ShopproHelper.HideBusyIndicator(this);

            // Now close the window
            this.Dispatcher.BeginInvoke(delegate()
            {
                Debug.WriteLine("Going to call Close().");
                // This will close this window automatically: http://msdn.microsoft.com/en-us/library/system.windows.controls.childwindow.close%28VS.95%29.aspx
                this.DialogResult = true;
                Debug.WriteLine("Called Close().");
            });
        }
        /// <summary>
        /// Thread body
        /// </summary>
        /// <param name="state"></param>
        private void StartWork(object state)
        {
            MediaView mediaView  = state as MediaView;
            media     savedMedia = null;
            media     theMedia   = new media();

            mediaView.Merge(theMedia, editMode);

            // Save to backend
            if (this.editMode == DataFormMode.Edit)
            {
                // Edit mode
                AutoResetEvent nextOneAutoResetEvent           = new AutoResetEvent(false);
                EventHandler <save_MediaCompletedEventArgs> h1 = (s, e) =>
                {
                    // TODO: handle error from server side
                    savedMedia = e.Result;
                    nextOneAutoResetEvent.Set();
                };
                Globals.WSClient.save_MediaCompleted += h1;
                Globals.WSClient.save_MediaAsync(theMedia);
                nextOneAutoResetEvent.WaitOne();
                Globals.WSClient.save_MediaCompleted -= h1;
            }
            else
            {
                //
                // Add new mode
                //

                // Get file content
                byte[] content = ShopproHelper.ReadFully(fileInfo.OpenRead());

                // Save to the backend
                AutoResetEvent nextOneAutoResetEvent = new AutoResetEvent(false);
                EventHandler <createNew_MediaCompletedEventArgs> h3 = (s, e) =>
                {
                    // TODO: handle error from server side
                    savedMedia = e.Result;
                    nextOneAutoResetEvent.Set();
                };
                Globals.WSClient.createNew_MediaCompleted += h3;
                Globals.WSClient.createNew_MediaAsync(theMedia, content);
                nextOneAutoResetEvent.WaitOne();
                Globals.WSClient.createNew_MediaCompleted -= h3;
            }

            // Check return result. If failure, savedCategory will be null
            if (savedMedia != null)
            {
                this.Dispatcher.BeginInvoke(delegate()
                {
                    mediaView.Restore(new MediaView(savedMedia));
                });
            }
            else
            {
                // Show error message
                ShopproHelper.ShowMessageWindow(this, "Error", "Fail to save this entry.", false);

                // Back to readonly mode
                this.Dispatcher.BeginInvoke(delegate()
                {
                    // Restore cached UI data
                    mediaView.CancelEdit();
                    this.mediaDataForm.CancelEdit();
                });
            }

            // No unsaved item
            this.editMode = DataFormMode.ReadOnly;
            this.fileInfo = null;

            // Hide busy indicator
            ShopproHelper.HideBusyIndicator(this);

            // Go to original page if necessary
            if (this.ContentPageCtx.GotoAddNewPage)
            {
                ShopproHelper.GoToContentPage(this, PageEnum.MediaListPage);
            }
        }