コード例 #1
0
        void onBackButtonClicked(object sender, EventArgs e)
        {
            if (currentQuestionCount > 0)
            {
                //Decrease current count to go back to previous question
                currentQuestionCount--;

                //Set user answer variable to equal to user's answer
                userAnswer = survey.surveyQuestions[currentQuestionCount].responseAnswer;

                //Set the visibility of the 'Back' and 'Next' buttons
                if (currentQuestionCount == 0)
                {
                    backButton.IsVisible   = false;
                    nextButton.IsVisible   = true;
                    submitButton.IsVisible = false;
                }
                else
                {
                    backButton.IsVisible = true;
                    if (currentQuestionCount < (survey.surveyQuestions.Count - 1))
                    {
                        nextButton.IsVisible   = true;
                        submitButton.IsVisible = false;
                    }
                    else
                    {
                        nextButton.IsVisible   = false;
                        submitButton.IsVisible = true;
                    }
                }
            }

            //Delete the last child (answer field)
            var lastChild = questionLayout.Children.Last();

            questionLayout.Children.Remove(lastChild);

            //Display previous question
            surveyQuestion surveyQuestion = survey.surveyQuestions[currentQuestionCount];

            surveyQuestionLbl.Text = surveyQuestion.questionTitle;

            //Create the answer field
            if (surveyQuestion.questionType == "dropdown" || surveyQuestion.questionType == "radiobuttons")
            {
                StackLayout stackLayout = new StackLayout();

                Frame frame = new Frame();
                frame.CornerRadius    = 5;
                frame.BackgroundColor = Color.FromHex("#E5E7E8");
                frame.Padding         = 2;
                frame.HasShadow       = false;

                //Create picker to store dropdown options
                Picker picker = new Picker();
                picker.Title = "- Select your answer -";
                picker.SelectedIndexChanged += OnPickerSelectedIndexChanged;
                picker.ItemsSource           = surveyQuestion.surveyOptions;
                picker.ItemDisplayBinding    = new Binding("answerTitle");

                foreach (surveyOption option in picker.ItemsSource)
                {
                    if (option.answerTitle == survey.surveyQuestions[currentQuestionCount].responseAnswer)
                    {
                        picker.SelectedItem = option;
                        break;
                    }
                }

                frame.Content = picker;
                stackLayout.Children.Add(frame);
                questionLayout.Children.Add(stackLayout);
            }
            else if (surveyQuestion.questionType == "shortsentence")
            {
                StackLayout stackLayout = new StackLayout();

                Frame frame = new Frame();
                frame.CornerRadius    = 5;
                frame.BackgroundColor = Color.FromHex("#E5E7E8");
                frame.Padding         = 2;
                frame.HasShadow       = false;

                Entry entry = new Entry();
                entry.TextChanged += OnTextChanged;
                if (survey.surveyQuestions[currentQuestionCount].responseAnswer != "")
                {
                    entry.Text = survey.surveyQuestions[currentQuestionCount].responseAnswer;
                }

                frame.Content = entry;
                stackLayout.Children.Add(frame);
                questionLayout.Children.Add(stackLayout);
            }
            else if (surveyQuestion.questionType == "longsentence")
            {
                StackLayout stackLayout = new StackLayout();

                Frame frame = new Frame();
                frame.CornerRadius    = 5;
                frame.BackgroundColor = Color.FromHex("#E5E7E8");
                frame.Padding         = 2;
                frame.HasShadow       = false;

                Editor editor = new Editor();
                editor.HeightRequest = 200;
                editor.TextChanged  += OnTextChanged;
                if (survey.surveyQuestions[currentQuestionCount].responseAnswer != "")
                {
                    editor.Text = survey.surveyQuestions[currentQuestionCount].responseAnswer;
                }

                frame.Content = editor;
                stackLayout.Children.Add(frame);
                questionLayout.Children.Add(stackLayout);
            }
            else if (surveyQuestion.questionType == "checkboxes")
            {
                StackLayout answerLayout = new StackLayout();

                //Console.WriteLine("TESTINGGGGGGGG" + surveyQuestion.surveyOptions[0].answerTitle);
                if (surveyQuestion.surveyOptions != null)
                {
                    for (int y = 0; y < surveyQuestion.surveyOptions.Count(); y++)
                    {
                        StackLayout stackLayout = new StackLayout()
                        {
                            Orientation = StackOrientation.Horizontal
                        };

                        Switch switcher = new Switch();
                        switcher.StyleId  = surveyQuestion.surveyOptions[y].answerID;
                        switcher.Toggled += switcher_Toggled;
                        stackLayout.Children.Add(switcher);
                        stackLayout.Children.Add(new Label()
                        {
                            Text = surveyQuestion.surveyOptions[y].answerTitle
                        });

                        if (survey.surveyQuestions[currentQuestionCount].responseAnswer != "")
                        {
                            if (survey.surveyQuestions[currentQuestionCount].responseAnswer == switcher.StyleId)
                            {
                                switcher.IsToggled = true;
                            }
                        }

                        answerLayout.Children.Add(stackLayout);
                    }
                }
                questionLayout.Children.Add(answerLayout);
            }

            /*if (currentQuestionCount > 0)
             * {
             *  currentQuestionCount--;
             *
             *  if (pollQuestionLbl.Text == survey.surveyQuestions[currentQuestionCount].questionTitle)
             *  {
             *      currentQuestionCount--;
             *  }
             * }
             *
             * if (currentQuestionCount <= 0)
             * {
             *  backButton.IsVisible = false;
             * } else
             * {
             *  backButton.IsVisible = true;
             *  nextButton.IsVisible = true;
             * }
             *
             * if (currentQuestionCount >= 0)
             * {
             *  //Delete the last child (answer field)
             *  var lastChild = questionLayout.Children.Last();
             *  questionLayout.Children.Remove(lastChild);
             *
             *  //Display previous question
             *
             *  surveyQuestion surveyQuestion = survey.surveyQuestions[currentQuestionCount];
             *  pollQuestionLbl.Text = surveyQuestion.questionTitle;
             *
             *  //Create the answer field
             *  if (surveyQuestion.questionType == "dropdown")
             *  {
             *      StackLayout stackLayout = new StackLayout();
             *
             *      Frame frame = new Frame();
             *      frame.CornerRadius = 5;
             *      frame.BackgroundColor = Color.FromHex("#E5E7E8");
             *      frame.Padding = 2;
             *      frame.HasShadow = false;
             *
             *      //Create picker to store dropdown options
             *      Picker picker = new Picker();
             *      picker.Title = "- Select your answer -";
             *      picker.SelectedIndexChanged += OnPickerSelectedIndexChanged;
             *      picker.ItemsSource = surveyQuestion.surveyOptions;
             *      picker.ItemDisplayBinding = new Binding("answerTitle");
             *
             *      frame.Content = picker;
             *      stackLayout.Children.Add(frame);
             *      questionLayout.Children.Add(stackLayout);
             *  }
             *  else if (surveyQuestion.questionType == "shortAnswer")
             *  {
             *      StackLayout stackLayout = new StackLayout();
             *
             *      Frame frame = new Frame();
             *      frame.CornerRadius = 5;
             *      frame.BackgroundColor = Color.FromHex("#E5E7E8");
             *      frame.Padding = 2;
             *      frame.HasShadow = false;
             *
             *      Entry entry = new Entry();
             *      entry.TextChanged += OnTextChanged;
             *      if (survey.surveyQuestions[currentQuestionCount].responseAnswer != "")
             *      {
             *          entry.Text = survey.surveyQuestions[currentQuestionCount].responseAnswer;
             *      }
             *
             *      frame.Content = entry;
             *      stackLayout.Children.Add(frame);
             *      questionLayout.Children.Add(stackLayout);
             *  }
             *  else if (surveyQuestion.questionType == "paragraph")
             *  {
             *      StackLayout stackLayout = new StackLayout();
             *
             *      Frame frame = new Frame();
             *      frame.CornerRadius = 5;
             *      frame.BackgroundColor = Color.FromHex("#E5E7E8");
             *      frame.Padding = 2;
             *      frame.HasShadow = false;
             *
             *      Editor editor = new Editor();
             *      editor.HeightRequest = 200;
             *      editor.TextChanged += OnTextChanged;
             *      if (survey.surveyQuestions[currentQuestionCount].responseAnswer != "")
             *      {
             *          editor.Text = survey.surveyQuestions[currentQuestionCount].responseAnswer;
             *      }
             *
             *      frame.Content = editor;
             *      stackLayout.Children.Add(frame);
             *      questionLayout.Children.Add(stackLayout);
             *  }
             *  else if (surveyQuestion.questionType == "checkbox")
             *  {
             *
             *  }
             *  else
             *  {
             *
             *  }
             * }
             * if (currentQuestionCount == 0)
             * {
             *  currentQuestionCount++;
             * }
             * Console.WriteLine("Current question count is: " + currentQuestionCount);*/
        }
コード例 #2
0
        void onNextButtonClicked(object sender, EventArgs e)
        {
            bool isErrorPresent = false;

            if (isFirstLoaded)
            {
                isFirstLoaded              = false;
                surveyTitleLbl.IsVisible   = false;
                surveyDescLbl.IsVisible    = false;
                firstPageBoxView.IsVisible = false;

                questionLayout.IsVisible = true;

                nextButton.Text = "Next";
            }
            else
            {
                if (userAnswer == "")
                {
                    //Error is present when input field is empty, user cannot proceed to next question when error is present
                    DisplayAlert("Answer field must not be empty", "Please fill in the answer field.", "OK");
                    isErrorPresent = true;
                }
                else
                {
                    //Reset user answer variable
                    userAnswer = "";
                    //Increase the current count and display the next questions
                    if (currentQuestionCount < survey.surveyQuestions.Count)
                    {
                        currentQuestionCount++;
                        if (currentQuestionCount == (survey.surveyQuestions.Count - 1))
                        {
                            nextButton.IsVisible   = false;
                            submitButton.IsVisible = true;
                        }
                        else
                        {
                            nextButton.IsVisible   = true;
                            submitButton.IsVisible = false;
                        }
                    }
                }
            }

            if (!isErrorPresent)
            {
                //Display the question when no error present
                surveyQuestion surveyQuestion = new surveyQuestion();
                surveyQuestion         = survey.surveyQuestions[currentQuestionCount];
                surveyQuestionLbl.Text = surveyQuestion.questionTitle;
                if (currentQuestionCount != 0)
                {
                    //Delete the last child (answer field)
                    var lastChild = questionLayout.Children.Last();
                    questionLayout.Children.Remove(lastChild);

                    //Display the back button when it's not the first question
                    backButton.IsVisible = true;
                }

                //Create the answer field
                if (surveyQuestion.questionType == "dropdown" || surveyQuestion.questionType == "radiobuttons")
                {
                    StackLayout stackLayout = new StackLayout();

                    Frame frame = new Frame();
                    frame.CornerRadius    = 5;
                    frame.BackgroundColor = Color.FromHex("#E5E7E8");
                    frame.Padding         = 2;
                    frame.HasShadow       = false;

                    //Create picker to store dropdown options
                    Picker picker = new Picker();
                    picker.Title = "- Select your answer -";
                    picker.SelectedIndexChanged += OnPickerSelectedIndexChanged;
                    picker.ItemsSource           = surveyQuestion.surveyOptions;
                    picker.ItemDisplayBinding    = new Binding("answerTitle");

                    if (picker.ItemsSource != null)
                    {
                        foreach (surveyOption option in picker.ItemsSource)
                        {
                            if (option.answerTitle == survey.surveyQuestions[currentQuestionCount].responseAnswer)
                            {
                                picker.SelectedItem = option;
                                break;
                            }
                        }
                    }

                    frame.Content = picker;
                    stackLayout.Children.Add(frame);
                    questionLayout.Children.Add(stackLayout);
                }
                else if (surveyQuestion.questionType == "shortsentence")
                {
                    StackLayout stackLayout = new StackLayout();

                    Frame frame = new Frame();
                    frame.CornerRadius    = 5;
                    frame.BackgroundColor = Color.FromHex("#E5E7E8");
                    frame.Padding         = 2;
                    frame.HasShadow       = false;

                    Entry entry = new Entry();
                    entry.TextChanged += OnTextChanged;
                    if (survey.surveyQuestions[currentQuestionCount].responseAnswer != "")
                    {
                        entry.Text = survey.surveyQuestions[currentQuestionCount].responseAnswer;
                    }

                    frame.Content = entry;
                    stackLayout.Children.Add(frame);
                    questionLayout.Children.Add(stackLayout);
                }
                else if (surveyQuestion.questionType == "longsentence")
                {
                    StackLayout stackLayout = new StackLayout();

                    Frame frame = new Frame();
                    frame.CornerRadius    = 5;
                    frame.BackgroundColor = Color.FromHex("#E5E7E8");
                    frame.Padding         = 2;
                    frame.HasShadow       = false;

                    Editor editor = new Editor();
                    editor.HeightRequest = 200;
                    editor.TextChanged  += OnTextChanged;
                    if (survey.surveyQuestions[currentQuestionCount].responseAnswer != "")
                    {
                        editor.Text = survey.surveyQuestions[currentQuestionCount].responseAnswer;
                    }

                    frame.Content = editor;
                    stackLayout.Children.Add(frame);
                    questionLayout.Children.Add(stackLayout);
                }
                else if (surveyQuestion.questionType == "checkboxes")
                {
                    //var layout = new StackLayout() { Orientation = StackOrientation.Horizontal };
                    StackLayout answerLayout = new StackLayout();
                    //Console.WriteLine("TESTINGGGGGGGG" + surveyQuestion.surveyOptions[0].answerTitle);
                    if (surveyQuestion.surveyOptions != null)
                    {
                        for (int y = 0; y < surveyQuestion.surveyOptions.Count(); y++)
                        {
                            StackLayout stackLayout = new StackLayout()
                            {
                                Orientation = StackOrientation.Horizontal
                            };

                            Switch switcher = new Switch();
                            switcher.StyleId  = surveyQuestion.surveyOptions[y].answerID;
                            switcher.Toggled += switcher_Toggled;
                            stackLayout.Children.Add(switcher);
                            stackLayout.Children.Add(new Label()
                            {
                                Text = surveyQuestion.surveyOptions[y].answerTitle
                            });

                            if (survey.surveyQuestions[currentQuestionCount].responseAnswer != "")
                            {
                                if (survey.surveyQuestions[currentQuestionCount].responseAnswer == switcher.StyleId)
                                {
                                    switcher.IsToggled = true;
                                }
                            }

                            answerLayout.Children.Add(stackLayout);
                        }
                    }
                    questionLayout.Children.Add(answerLayout);
                }
            }

            /*
             * if (userAnswer == "" && surveyTitleLbl.IsVisible == false)
             * {
             *  DisplayAlert("Answer field must not be empty", "Please fill in the answer field.", "OK");
             * } else
             * {
             *  //Set the answer of the survey question to user's answer and reset the userAnswer variable
             *  if (currentQuestionCount > 0)
             *  {
             *      survey.surveyQuestions[currentQuestionCount - 1].responseAnswer = userAnswer;
             *      userAnswer = "";
             *  }
             *  //Set the labels displaying the survey details to invisible and set the Back button to visible after the user starts the survey
             *  if (surveyTitleLbl.IsVisible == true)
             *  {
             *      surveyTitleLbl.IsVisible = false;
             *      surveyDescLbl.IsVisible = false;
             *      firstPageBoxView.IsVisible = false;
             *
             *      questionLayout.IsVisible = true;
             *
             *      nextButton.Text = "Next";
             *  }
             *
             *  //Display question
             *  if (currentQuestionCount < survey.surveyQuestions.Count)
             *  {
             *      if (pollQuestionLbl.Text == survey.surveyQuestions[currentQuestionCount].questionTitle && (currentQuestionCount+1) < survey.surveyQuestions.Count)
             *      {
             *          currentQuestionCount++;
             *      }
             *      //Console.WriteLine("Current question count is: " + currentQuestionCount);
             *      surveyQuestion surveyQuestion = survey.surveyQuestions[currentQuestionCount];
             *      pollQuestionLbl.Text = surveyQuestion.questionTitle;
             *      if (currentQuestionCount != 0)
             *      {
             *          //Delete the last child (answer field)
             *          var lastChild = questionLayout.Children.Last();
             *          questionLayout.Children.Remove(lastChild);
             *
             *          //Display the back button when it's not the first question
             *          backButton.IsVisible = true;
             *      }
             *
             *      //Create the answer field
             *      if (surveyQuestion.questionType == "dropdown")
             *      {
             *          StackLayout stackLayout = new StackLayout();
             *
             *          Frame frame = new Frame();
             *          frame.CornerRadius = 5;
             *          frame.BackgroundColor = Color.FromHex("#E5E7E8");
             *          frame.Padding = 2;
             *          frame.HasShadow = false;
             *
             *          //Create picker to store dropdown options
             *          Picker picker = new Picker();
             *          picker.Title = "- Select your answer -";
             *          picker.SelectedIndexChanged += OnPickerSelectedIndexChanged;
             *          picker.ItemsSource = surveyQuestion.surveyOptions;
             *          picker.ItemDisplayBinding = new Binding("answerTitle");
             *
             *          frame.Content = picker;
             *          stackLayout.Children.Add(frame);
             *          questionLayout.Children.Add(stackLayout);
             *      }
             *      else if (surveyQuestion.questionType == "shortAnswer")
             *      {
             *          StackLayout stackLayout = new StackLayout();
             *
             *          Frame frame = new Frame();
             *          frame.CornerRadius = 5;
             *          frame.BackgroundColor = Color.FromHex("#E5E7E8");
             *          frame.Padding = 2;
             *          frame.HasShadow = false;
             *
             *          Entry entry = new Entry();
             *          entry.TextChanged += OnTextChanged;
             *          if (survey.surveyQuestions[currentQuestionCount].responseAnswer != "")
             *          {
             *              entry.Text = survey.surveyQuestions[currentQuestionCount].responseAnswer;
             *          }
             *
             *          frame.Content = entry;
             *          stackLayout.Children.Add(frame);
             *          questionLayout.Children.Add(stackLayout);
             *      }
             *      else if (surveyQuestion.questionType == "paragraph")
             *      {
             *          StackLayout stackLayout = new StackLayout();
             *
             *          Frame frame = new Frame();
             *          frame.CornerRadius = 5;
             *          frame.BackgroundColor = Color.FromHex("#E5E7E8");
             *          frame.Padding = 2;
             *          frame.HasShadow = false;
             *
             *          Editor editor = new Editor();
             *          editor.HeightRequest = 200;
             *          editor.TextChanged += OnTextChanged;
             *          if (survey.surveyQuestions[currentQuestionCount].responseAnswer != "")
             *          {
             *              editor.Text = survey.surveyQuestions[currentQuestionCount].responseAnswer;
             *          }
             *
             *          frame.Content = editor;
             *          stackLayout.Children.Add(frame);
             *          questionLayout.Children.Add(stackLayout);
             *      }
             *      else if (surveyQuestion.questionType == "checkbox")
             *      {
             *
             *      }
             *      else
             *      {
             *
             *      }
             *
             *      currentQuestionCount++;
             *
             *      if (currentQuestionCount >= survey.surveyQuestions.Count)
             *      {
             *          nextButton.IsVisible = false;
             *      }
             *      else
             *      {
             *          nextButton.IsVisible = true;
             *      }
             *  }
             * }
             * Console.WriteLine("Current question count is: " + currentQuestionCount);
             */
        }