private void AddPromptElementsButton_Click(object sender, RoutedEventArgs e)
        {
            PromptElement currentPromptElement = new PromptElement(MyPromptElementsStack.Children.Count, MyCounter);

            currentPromptElement.Name = "myPromptElement" + MyCounter;
            MyCounter++;

            MyPromptElementsStack.Children.Add(currentPromptElement);
        }
        private void InsertPromptElementsButton_Click(object sender, RoutedEventArgs e)
        {
            // check if the value is valid
            string value = MyInsertIndexText.Text;
            int    index;

            if (!int.TryParse(value, out index))
            {
                MyInsertIndexText.Text = ""; // clear the text box
                return;                      // do nothing
            }

            //
            int count = MyPromptElementsStack.Children.Count;

            // case: stack is empty
            if (count == 0)
            {
                PromptElement currentPromptElement = new PromptElement(0, MyCounter);
                currentPromptElement.Name = "myPromptElement" + MyCounter;
                MyCounter++;

                MyPromptElementsStack.Children.Add(currentPromptElement);
            }

            // case: index exceeds stack size
            else if (index >= count)
            {
                PromptElement currentPromptElement = new PromptElement(count, MyCounter);
                currentPromptElement.Name = "myPromptElement" + MyCounter;
                MyCounter++;

                MyPromptElementsStack.Children.Add(currentPromptElement);
            }

            //
            else
            {
                PromptElement currentPromptElement = new PromptElement(index, MyCounter);
                currentPromptElement.Name = "myPromptElement" + MyCounter;
                MyCounter++;

                //
                MyPromptElementsStack.Children.Insert(index, currentPromptElement);
                for (int i = index + 1; i < MyPromptElementsStack.Children.Count; ++i)
                {
                    var promptElement = (PromptElement)MyPromptElementsStack.Children[i];
                    promptElement.PositionName = "" + i;
                }
            }

            this.MyInsertIndexText.Text = "";
        }
        private async void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            // get the title and randomization
            string title    = MyTitleText.Text;
            bool   isRandom = MyRandomizeToggle.IsOn;

            // validate title
            if (title.Equals(""))
            {
                var dialog = new MessageDialog("ERROR: You are missing the title.");
                await dialog.ShowAsync();

                return;
            }

            // get the prompts
            var promptElements = MyPromptElementsStack.Children;

            // validate the prompt size
            if (promptElements.Count == 0)
            {
                var dialog = new MessageDialog("ERROR: You have no prompts.");
                await dialog.ShowAsync();

                return;
            }

            // iterate through each prompt
            var promptElementDataList = new List <Tuple <string, string, int, string> >();

            for (int i = 0; i < promptElements.Count; ++i)
            {
                // get the current prompt
                PromptElement promptElement = (PromptElement)promptElements[i];

                // get the prompt's contents
                string imageFileName  = promptElement.ImageFileName;
                string labelName      = promptElement.LabelName;
                string iterationsText = promptElement.IterationsText;
                string displayType    = promptElement.IsDisplayTrace ? "trace" : promptElement.IsDisplayReference ? "reference" : "none";

                // validate the prompt's contents
                bool   isValid      = true;
                string errorMessage = "";
                int    iterations   = 1;
                if (imageFileName.Equals("") && !displayType.Equals("none"))
                {
                    isValid      = false;
                    errorMessage = "ERROR: One of your prompts is missing an image file name.";
                }
                else if (labelName.Equals(""))
                {
                    isValid      = false;
                    errorMessage = "ERROR: One of your prompts is missing a label name.";
                }
                else if (!int.TryParse(iterationsText, out iterations))
                {
                    isValid      = false;
                    errorMessage = "ERROR: One of your prompts uses a non-number for iterations.";
                }
                else if (iterations <= 0)
                {
                    isValid      = false;
                    errorMessage = "ERROR: One of your prompts uses a non-positive number for iterations.";
                }
                if (!isValid)
                {
                    var dialog = new MessageDialog(errorMessage);
                    await dialog.ShowAsync();

                    return;
                }

                // get the prompt element's data
                var promptElementData = new Tuple <string, string, int, string>(imageFileName, labelName, iterations, displayType);
                promptElementDataList.Add(promptElementData);
            }

            // get save file
            FileSavePicker savePicker = new FileSavePicker();

            savePicker.FileTypeChoices.Add("eXtensible Markup Language (XML) file", new List <string>()
            {
                ".xml"
            });
            savePicker.SuggestedFileName = "New Document";
            StorageFile file = await savePicker.PickSaveFileAsync();

            if (file == null)
            {
                return;
            }

            // write to XML file
            WriteToXml(file, title, isRandom, promptElementDataList);

            // show success message dialog
            var successDialog = new MessageDialog("File saved successfully.", "Success");
            await successDialog.ShowAsync();
        }