1

基本クラスでは非同期ではないが、派生クラスでは非同期である仮想メソッドを定義したいのですが、呼び出し元はデリゲート(実際には画面上のボタンによってアクティブ化されるICommand)を使用してそれを呼び出します。これを行うにはどうすればよいですか。

public class BaseClass
{
    BIONESSBindingCommand mLogoffCommand;

    public ICommand LogoffCommand
    {
        get
        {
            if (mLogoffCommand == null)
            {
                mLogoffCommand = new BIONESSBindingCommand(
                    Param => Logoff(), //Command DoWork
                    Param => true); //Always can execute
            }

            return mLogoffCommand;
        }
    }

    public virtual Task Logoff()
    {
        DoLogoff();
        return null; //???
    }
}

public class DerivedClass : BaseClass
{
    public override async Task Logoff()
    {
        await SomeWoAsyncWork();
        base.Logoff(); //Has warninngs
    }
}
4

1 に答える 1

6

Call Task.FromResult to get a completed Task. Also, await it in the derived class (this will enable error propagation).

public class BaseClass
{
  public virtual Task Logoff()
  {
    DoLogoff();
    return Task.FromResult(false);
  }
}

public class DerivedClass : BaseClass
{
  public override async Task Logoff()
  {
    await SomeWoAsyncWork();
    await base.Logoff();
  }
}
于 2012-07-05T15:29:47.533 に答える