返回类型必须为“布尔”错误

时间:2018-07-22 13:53:58

标签: c# xamarin async-await

我已经创建了使用xamarin android的应用程序,需要显示和警报框,并基于所选的值进行一些计算。因此在菜单项上,单击“我已添加以下代码”。

public override async Task<bool> OnOptionsItemSelected(IMenuItem item)
{
    bool bTotalResult = false;
    switch (item.ItemId)
    {


        case Resource.Id.action_next:
            bTotalResult = await ShowPayMentTypeDialog();

            return await Task.Run(() => true).ConfigureAwait(true); ;
        default:
            return await Task.Run(() => base.OnOptionsItemSelected(item)).ConfigureAwait(true);;
    }
    //return await Task.Run(() => false ).ConfigureAwait(true);
}
private async Task<bool> ShowPayMentTypeDialog()
{
    bool bResult = false;
    try
    {
        LayoutInflater layoutInflater = LayoutInflater.From(this);
        View view = layoutInflater.Inflate(Resource.Layout.lyt_cash_credit_popup, null);
        Android.Support.V7.App.AlertDialog.Builder alertbuilder = new Android.Support.V7.App.AlertDialog.Builder(this);
        alertbuilder.SetView(view);
        var userdata = view.FindViewById<Spinner>(Resource.Id.spnrPaymentTerms);
        List<cls_spinner_adapter> resultsWayNo = new List<cls_spinner_adapter> {
        new cls_spinner_adapter {SZ_SPINNER_TEXT ="CASH"},
       new cls_spinner_adapter {SZ_SPINNER_TEXT ="CREDIT" } };
        CommonSpinnerAdapter paymentAdapter = new CommonSpinnerAdapter(this, resultsWayNo);
        userdata.Adapter = paymentAdapter;
        alertbuilder.SetCancelable(false)
        .SetPositiveButton("OK", delegate
        {
            if (resultsWayNo[userdata.SelectedItemPosition].SZ_SPINNER_TEXT == "CASH")
            {
                cls_statics.B_IS_PROMOMAYMANET_CASH = true;
            }
            else
            {
                cls_statics.B_IS_PROMOMAYMANET_CASH = false;
            }
            bResult = true;
        })
        .SetNegativeButton("Cancel", delegate
        {
            cls_statics.B_IS_PROMOMAYMANET_CASH = false;
            bResult = true;
            alertbuilder.Dispose();

        });
        Android.Support.V7.App.AlertDialog dialog = alertbuilder.Create();
        dialog.Show();
    }
    catch
    {

    }
    return await Task.Run(() => bResult).ConfigureAwait(true);
}

但是在编译时,我遇到了错误。

  

返回类型必须为'bool'才能匹配覆盖的成员Activity.OnOptionsItemSelected(IMenuItem)

1 个答案:

答案 0 :(得分:3)

已发布的代码存在几个问题,让我们解决它们。

  1. 正如编译器告诉您的那样,您不能更改要覆盖的方法的签名。因此,将您的方法更改为:

    override bool OnOptionsItemSelected
    
  2. .ConfigureAwait(true);默认配置,无需显式调用它。

  3. 以下行不必要地启动新的线程池线程,只是为了返回恒定值:

    return await Task.Run(() => true)
    

    该代码只能是:

    return true;
    
  4. 不应将同步代码标记为异步,以使它们异步。相反,如果需要,调用者必须以异步方式调用它们。您应该熟悉Stephen Cleary的这篇出色文章:Task.Run Etiquette Examples: Don't Use Task.Run in the Implementation

因此,让我们将所有这些放在一起:

//Point 1
public override bool OnOptionsItemSelected(IMenuItem item)
{
    bool bTotalResult = false;

    switch (item.ItemId)
    {
        case Resource.Id.action_next:
            bTotalResult = Task.Run(() => ShowPayMentTypeDialog()).Result;

            // point 3
            return true;
        default:
            // point 3
            return base.OnOptionsItemSelected(item);
    }
}

// point 4
private bool ShowPayMentTypeDialog()
{
    bool bResult = false;
    try
    {
        LayoutInflater layoutInflater = LayoutInflater.From(this);
        View view = layoutInflater.Inflate(Resource.Layout.lyt_cash_credit_popup, null);
        Android.Support.V7.App.AlertDialog.Builder alertbuilder = new Android.Support.V7.App.AlertDialog.Builder(this);
        alertbuilder.SetView(view);
        var userdata = view.FindViewById<Spinner>(Resource.Id.spnrPaymentTerms);
        List<cls_spinner_adapter> resultsWayNo = new List<cls_spinner_adapter> {
        new cls_spinner_adapter {SZ_SPINNER_TEXT ="CASH"},
       new cls_spinner_adapter {SZ_SPINNER_TEXT ="CREDIT" } };
        CommonSpinnerAdapter paymentAdapter = new CommonSpinnerAdapter(this, resultsWayNo);
        userdata.Adapter = paymentAdapter;
        alertbuilder.SetCancelable(false)
        .SetPositiveButton("OK", delegate
        {
            if (resultsWayNo[userdata.SelectedItemPosition].SZ_SPINNER_TEXT == "CASH")
            {
                cls_statics.B_IS_PROMOMAYMANET_CASH = true;
            }
            else
            {
                cls_statics.B_IS_PROMOMAYMANET_CASH = false;
            }
            bResult = true;
        })
        .SetNegativeButton("Cancel", delegate
        {
            cls_statics.B_IS_PROMOMAYMANET_CASH = false;
            bResult = true;
            alertbuilder.Dispose();

        });
        Android.Support.V7.App.AlertDialog dialog = alertbuilder.Create();
        dialog.Show();
    }
    catch
    {

    }

    // point 3
    return bResult;
}