示例#1
0
        async void OnUpdateButtonClicked(object sender, EventArgs e)
        {
            bool   isErrorPresent = false;
            string errorMessage   = "";
            string pollQuestion   = "";

            //Validate user inputs
            if (pollQuestionInput.Text == "")
            {
                isErrorPresent = true;
                errorMessage   = "Please enter poll question. ";
            }

            if (selectedOption == "")
            {
                isErrorPresent = true;
                errorMessage   = "Please enter number of poll option. ";
            }

            //Obtain the inputs for options
            string[] options = new string[optionListLayout.Children.Count()];
            int      count   = 0;

            foreach (StackLayout layout in optionListLayout.Children)
            {
                foreach (View view in layout.Children)
                {
                    if (view.GetType() == typeof(Frame))
                    {
                        if (((Frame)view).Content.GetType() == typeof(Entry))
                        {
                            Entry entry = (Entry)((Frame)view).Content;
                            options[count] = entry.Text;
                            count++;
                        }
                    }
                }
            }

            if (options.Count() <= 0)
            {
                isErrorPresent = true;
                errorMessage   = "Please enter poll option fields. ";
            }

            if (!isErrorPresent)
            {
                //If inputs are valid, proceed to update the poll
                if (adminAuth.Username != null && adminAuth.Password != null)
                {
                    activityIndicator.IsVisible = true;
                    activityIndicator.IsRunning = true;

                    string httpTask = await Task.Run <string>(() => HttpRequestHandler.PostUpdatePoll(adminAuth.Username, adminAuth.Password, pollQuestionInput.Text, selectedOption, options, currentPoll));

                    string httpResult = httpTask.ToString();

                    activityIndicator.IsVisible = false;
                    activityIndicator.IsRunning = false;
                    if (httpResult == "Successfully updated the poll!")
                    {
                        await DisplayAlert("Success", "Poll has been successfully updated!", "OK");

                        var page     = App.Current.MainPage as rootPage;
                        var pollPage = new pollPage();
                        page.changePage(pollPage);
                    }
                    else
                    {
                        await DisplayAlert("Failed", httpResult, "OK");
                    }
                }
                else
                {
                    Console.WriteLine("User is not logged in");
                }
            }
            else
            {
                await DisplayAlert("Failed", errorMessage, "OK");
            }
        }