当我向表单添加自动完成字段时,为什么不提交表单?

时间:2019-02-11 09:46:19

标签: xamarin.forms

我有一个可以正常工作的表格,并且将所有正确的数据提交给了azure,并且下一页正在查看添加的所有“药物”。该表单可以正常工作,直到您添加了“诊断”,然后从数据库中添加的“诊断”中拉出了该表单,似乎可以很好地将其拉出,但是当我尝试使用选定的“诊断”来提交表单时,查看全部医学不再更新,医学将不会在那里,但是如果我不添加“诊断”就可以正常工作。

<summary>
    Click Method for Adding medication
</summary>
<param name="sender">Sender.</param>
<param name="e">E.</param>
async void AddMed_Clicked(object sender, EventArgs e)
{


    ///<summary>
    /// Validate inmputs in all fiekds
    /// </summary>
    if (txtMedicineName.Text == null)
    {
        await DisplayAlert("Enter Medication Name", "Please enter a medication name", "OK");
        txtMedicineName.Focus();
        txtMedicineName.ErrorText = "Enter Medication Name";
    }

    else if (txtPackSize.Text == null)
    {
        await DisplayAlert("Enter Medication Pack Size", "Please enter a medication pack size", "OK");
        txtPackSize.Focus();
        txtPackSize.ErrorText = "Please enter a pack size";
    }

    else if (txtFrequency.Text == null)

    {
        await DisplayAlert("Enter Medication Frequency", "Please enter medication frequency", "OK");
        txtFrequency.Focus();
        txtFrequency.ErrorText = "Enter medication frequency";
    }

    else if (txtDirections.Text == null)

    {
        await DisplayAlert("Enter Medication Directions", "Please enter medication directions", "OK");
        txtDirections.Focus();
        txtDirections.ErrorText = "Enter medicaton directions";
    }

    else if (MedsToEdit.Count > 0)

    {

        userMedication.Id = UserMedsToEdit[0].Id;
        userMedication.UserID = Helpers.Settings.UserKey;
        userMedication.Medname = txtMedicineName.Text;

        userMedication.IsActive = true;


        if (swtchRemnders.IsToggled == true)
        {
            userMedication.Reminder = true;
        }

        else

        {
            userMedication.Reminder = false;
        }

        //if (swtchFeedback.IsToggled == true)

        //{
        //  userMedication.CommunityFeedback = true;

        //}

        //else

        //{

        //  userMedication.CommunityFeedback = false;
        //}

        userMedication.Frequency = Convert.ToInt32(txtFrequency.Text);


        //userMedication.FrequencyUnit 
        userMedication.Directions = txtDirections.Text;
        ///userMedication.DateTimeToTake = dteReminderTime.Time.ToString();


        try
        {
            //Attempts insert (edit) and informs user if successfull
            await medicinemanager.AddMedicine(medToAdd);



            //userMedication.MedicationID = medToAdd.Id;

            await usermedicationmanager.UpdateUserMed(userMedication);
            await DisplayAlert("Medicine Updated", "Medicine successfully Updated", "OK");

            //If the reminder switch has been toggled, shcedule a notification

            NotificationReminder.Userid = Helpers.Settings.UserKey;
            NotificationReminder.Usermedid = userMedication.Id;
            NotificationReminder.MessageType = notifications.MessageTypes.InitialFeedBackReminder;

           // DateTime ReminderTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, dteReminderTime.Time.Hours, dteReminderTime.Time.Minutes, dteReminderTime.Time.Seconds);
            //TimeSpan NotificationTime = DateTime.Now - ReminderTime;


            //Insert into Notifications table

            try
            {
                await notificationsmanager.AddNotification(NotificationReminder);

                //if the insert into the notifications table was successfull, create the notification

                if (NotificationReminder.Id != null)
                {
                  //  medicinemanager.SetMedicineReminderNotification(NotificationTime, NotificationReminder.Userid, NotificationReminder.Id);
                }
            }


            catch (Exception ex)
            {
                //if there is an error - show it to the user
                await DisplayAlert("Error", ex.ToString(), "OK");
            }

            //Take the user to the confirmation code screen
            await Navigation.PushAsync(new addmedtimes(userMedication)); 
            Navigation.RemovePage(this);
        }
        catch (Exception ex)
        {
            //TODO: Remove ex variable and replace with user friendly string on production
            //Show Error
            await DisplayAlert("Error", ex.ToString(), "OK");
        }
    }

    else


    {
        medToAdd.MedName = txtMedicineName.Text;
        //medToAdd.MedDesc = txtMedicineDescription.Text;



        medToAdd.Packsize = txtPackSize.Text;
        medToAdd.Medfreq = txtFrequency.Text;
        medToAdd.Directions = txtDirections.Text;
        medToAdd.Status = "pending";

        userMedication.UserID = Helpers.Settings.UserKey;

        userMedication.IsActive = true;
        //userMedication.Reminder = true;

        if (swtchRemnders.IsToggled == true)
        {
            userMedication.Reminder = true;
        }

        else

        {
            userMedication.Reminder = false;
        }

    // if (swtchFeedback.IsToggled == true)

        //{
        //  userMedication.CommunityFeedback = true;

        //}

        //else

        //{

        //  userMedication.CommunityFeedback = false;
        //}

        userMedication.Frequency = Convert.ToInt32(txtFrequency.Text);
        //userMedication.FrequencyUnit 
        userMedication.Directions = txtDirections.Text;
        //userMedication.DateTimeToTake = dteReminderTime.Time.ToString();


        try
        {
            btnAdd.IsEnabled = false;
            //Attempts insert and informs user if successfull
            await medicinemanager.AddMedicine(medToAdd);


            userMedication.MedicationID = medToAdd.Id;
            userMedication.UserID = Helpers.Settings.UserKey;
            userMedication.Medname = txtMedicineName.Text;
            userMedication.Medpacksize = Convert.ToInt32(txtPackSize.Text);

            await usermedicationmanager.AddUserMedicine(userMedication);
            await DisplayAlert("Medicine Added", "Medicine successfully added", "OK");


            await SetMedReminders(MedTimes, userMedication);


            DateTime now = DateTime.Now;
            int currentHour = now.Hour;
            int TimetoTakeHours = Convert.ToDateTime(userMedication.DateTimeToTake).Hour;
            TimeSpan diff = now - Convert.ToDateTime(userMedication.DateTimeToTake);

            NotificationReminder.Userid = Helpers.Settings.UserKey;
            NotificationReminder.Usermedid = userMedication.Id;
            NotificationReminder.MessageType = notifications.MessageTypes.InitialFeedBackReminder;


            await Navigation.PushAsync(new addmedtimes(userMedication.Frequency, medToAdd, userMedication.Id)); //Take the user to the confirmation code screen
            await SendApprovalEmail(medToAdd.MedName, DateTime.Now.ToString());
            Navigation.RemovePage(this);
        }
        catch (Exception ex)
        {
            //TODO: Remove ex variable and replace with user friendly string on production
            //Show Error
            await DisplayAlert("Error", ex.ToString(), "OK");
            btnAdd.IsEnabled = true;
        }
    }
}

此代码可以诊断,但是不确定是否正确地将其添加到数据库中。

/// <summary>
/// Gets the users diagnosis from the db
/// </summary>
/// <returns>The user diagnosis.</returns>
async Task GetUserDiagnosis(bool isEdit)
{

    //UserDiag = await userdiagmanager.GetUserDiagnosis();

    try
    {
        var userdiag = await userdiagmanager.CurrentClient.InvokeApiAsync<IEnumerable<UserDiagnosis>>("getuserdiagnosis?userid=" + Helpers.Settings.UserKey + "", System.Net.Http.HttpMethod.Get, null);
        foreach (var item in userdiag)
        {
            UserDiag.Add(item);
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex);
    }



    if (UserDiag.Count > 0)
    {
        userdiagautocomplete.DataSource = UserDiag;

        userdiagautocomplete.DisplayMemberPath = "Diagnosistext";
        userdiagautocomplete.SelectedValuePath = "Id";
    }

    userdiagautocomplete.SelectionChanged += (sender, e) =>
    {

        if (e.Value != "")
        {
            UserDiagnosis selectedUserDiag = (UserDiagnosis)e.Value;
            UserMedtoEdit.Userdiagnosisid = selectedUserDiag.Id;
            userMedication.Userdiagnosisid = selectedUserDiag.Id;

        }

    };

    if (isEdit == true)
    {
        userdiagautocomplete.SelectedItem = UserMedtoEdit.Userdiagnosisid;
    }

}

async Task SetDiagnosisDropdown(string ID)
{

    //UserDiag = await userdiagmanager.GetUserDiagnosis();

    try
    {
        var userdiag = await userdiagmanager.CurrentClient.InvokeApiAsync<IEnumerable<UserDiagnosis>>("getuserdiagnosis?userid=" + Helpers.Settings.UserKey + "", System.Net.Http.HttpMethod.Get, null);
        foreach (var item in userdiag)
        {
            UserDiag.Add(item);
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex);
    }



    if (UserDiag.Count > 0)
    {
        userdiagautocomplete.DataSource = UserDiag;

        userdiagautocomplete.DisplayMemberPath = "Diagnosistext";
        userdiagautocomplete.SelectedValuePath = "Id";
    }

    userdiagautocomplete.SelectionChanged += (sender, e) =>
    {

        if (e.Value != "")
        {
            UserDiagnosis selectedUserDiag = (UserDiagnosis)e.Value;
            UserMedtoEdit.Userdiagnosisid = selectedUserDiag.Id;
            userMedication.Userdiagnosisid = selectedUserDiag.Id;

        }

    };

userdiagautocomplete.SelectedItem = ID;
}

0 个答案:

没有答案