无法将类型'System.Threading.Tasks.Task'隐式转换为'string'

时间:2019-06-03 15:46:03

标签: c# xamarin xna

我想从ShowKeyboard(...)中的变量“ inputtext”中获取字符串,但是我不知道该怎么做。在这行代码中,我总是收到错误消息:

KeyboardTextUsername = NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false);
  

错误CS0029:无法隐式转换类型   将'System.Threading.Tasks.Task'转换为'string'

编辑:更改代码后,我收到以下错误消息:

KeyboardTextUsername = await NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false);
  

错误CS4033:“ await”运算符只能在异步方法中使用。考虑使用“异步”修饰符标记此方法,并将其返回类型更改为“任务”。

我在做什么错?我不知道如何解决这个问题。

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        TouchPanel.EnabledGestures = GestureType.Tap;
        UsernameRectangle = new Microsoft.Xna.Framework.Rectangle(400, 230, 150, 100);
        CursorRectangle = new Microsoft.Xna.Framework.Rectangle(-150, -100, 10, 10);
    }

    protected override void Update(GameTime gameTime)
    {
        while (TouchPanel.IsGestureAvailable)
        {
            GestureSample gs = TouchPanel.ReadGesture();
            switch (gs.GestureType)
            {
            case GestureType.Tap:
              CursorRectangle = new Microsoft.Xna.Framework.Rectangle((int)gs.Position.X, (int)gs.Position.Y, 10, 10);
              CheckButtonPressed = true;
            break;
            }
        }

    if ((UsernameRectangle.Intersects(CursorRectangle)) && (CheckButtonPressed == true))
    {
        KeyboardTextUsername = await NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false);               
    }

    CheckButtonPressed = false;

        base.Update(gameTime);
    }


    public async Task<string> NewGetKeyboard(string Token, string MessageBoxTitle, string MessageBoxDescription, string Text, bool IsPassword)
    {
        string keyboardtext = "";
        string savedtext = await Getlogin(Token);
        if (savedtext == "")
            savedtext = Text;
        keyboardtext = await ShowKeyboard(Token, MessageBoxTitle, MessageBoxDescription, savedtext, IsPassword);

        return keyboardtext;
    }

    public async Task<string> Getlogin(string token)
    {
        string Text = "";
        try
        {
            Text = await SecureStorage.GetAsync(token);
        }
        catch (Exception ex)
        {
            // Possible that device doesn't support secure storage on device.
            Console.WriteLine("secure storage not supported on this device");
        }

        return Text;
    }

    private async Task<string> ShowKeyboard(string token, string messageboxtitle, string messageboxdescription, string text, bool ispassword)
    {
        string inputtext = "";
        await Task.Run(async () =>
        {           
            var result = await KeyboardInput.Show(messageboxtitle, messageboxdescription, text, ispassword);
            if (null != result)
            {
                inputtext = result;
            }
        });

        try
        {
            await SecureStorage.SetAsync(token, inputtext);
        }
        catch (Exception ex)
        {
            // Possible that device doesn't support secure storage on device.
            Console.WriteLine("secure storage not supported on this device");
        }

        return inputtext;
    }

3 个答案:

答案 0 :(得分:0)

NewGetKeyboard是一种async方法,应使用await关键字调用它

protected void async MyButtonClickHandler(object sender, EventArgs args)
{
    ...
    KeyboardTextUsername = await NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false);
    ...
}

答案 1 :(得分:0)

您不能只在诸如此类的方法之外调用await,这同样适用于条件。它必须在异步方法之内

public async Task<string> CallNewGetKeyboard(UsernameRectangle userRec, bool CheckButtonPressed, string tokenUserName, string messageBoxTitle, string messageBoxDescription, string messageBoxText, bool isPassword)
{
    if ((userRec.Intersects(CursorRectangle)) && (CheckButtonPressed == true))
    {
        var KeyboardTextUsername = await NewGetKeyboard(tokenUsername, messageBoxTitle, messageBoxDescription, messageBoxText, isPassword);
        return KeyboardTextUsername;    
    }
}

答案 2 :(得分:-2)

如果您不想使其异步

KeyboardTextUsername = NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false).GetAwaiter().GetResult();