在我对Xamarin.Forms的操作中添加一个取消按钮

时间:2019-02-18 14:26:21

标签: xamarin xamarin.forms

我正在尝试在我的显示警报上添加一个取消选项,以取消该操作。当前,当用户单击“提交”按钮时,数据将发布到azure数据库中,并且显示消息为“已添加反馈,已添加您的症状反馈”,用户可以选择“确定”或“取消”,但数据为已经添加,他们什么也不做。有没有一种方法可以设置它,以便用户必须选择“确定”来添加数据并可以选择“取消”选项来取消操作。这是我当前的代码,用于将数据提交到数据库:

 async Task AddSymptomFeedback(string usersymid, string value)
    {

        symptomFeedback.Usersymptomid = usersymid;
        symptomFeedback.Intensity = value;
        symptomFeedback.UserID = Helpers.Settings.UserKey;
        symptomFeedback.DateTime = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
        symptomFeedback.Datetimelimit = DateTime.Now.AddMinutes(30).ToString("g");

        try
        {

            await symptomFeedbackManager.AddSymptomfeedback(symptomFeedback);
           await DisplayAlert("Feedback Added", "Your symptom feedback has been added", "OK", "Cancel");



            await Navigation.PushAsync(new newviewsymptom());
            Navigation.RemovePage(this);
            HasAdjustedSl1ider = false;

            //We have to null the id so that we are not always updating the existing feedback item
            symptomFeedback.Id = null;

            //Refresh the List View 
            //await GetUserSymptoms();
        }
        catch (Exception ex)
        {
            await DisplayAlert("Error", ex.ToString(), "OK");
        }
    }

这是我的“提交”按钮后面的代码:

       async void SubmitFeedback_Clicked(object sender, EventArgs e)
    {


         if (rangeSlider == null)
        {
            await DisplayAlert("Add feedback", "Please add feedback for the symptom", "OK");
        }

        else

        {
            await AddSymptomFeedback(rangeSlider.IDValue, rangeSlider.Value.ToString());
        }

    }

1 个答案:

答案 0 :(得分:0)

您可以传递两个参数OkCancel。处理两个按钮的点击情况如下

var response = await DisplayAlert("Title", "Message", "Ok", "Cancel");
if (response == true) {
//Yes action
} 
else 
{
//No action
}

OP询问如何使用

async void SubmitFeedback_Clicked(object sender, EventArgs e)
{   
var response = await DisplayAlert("Title", "Message", "Ok", "Cancel");
if (response == true)
{
//Yes action
await AddSymptomFeedback(rangeSlider.IDValue, rangeSlider.Value.ToString());
} 
else 
{
//No action
}
}