Search code examples
c#xamarinxna

Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'string'


I want to get the string from the variable "inputtext" in ShowKeyboard(...) but I don't know how to do that. I always get an error message in this line of code:

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

Error CS0029: Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'string'

EDIT: After changing the code, I get this error message:

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

Error CS4033: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

What am I doing wrong? I don't know how to resolve this problem.

    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;
    }

Solution

  • You can't just call await outside of a method like that, that goes for your conditions as well. await needs to be within an async method; and conditions need to be within a method (be it async, or regular (sync) method). In this case, it'll reside in your async method.

    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;    
        }
    }
    

    Here's a bit of a shorter version of the above code..

    public async Task<string> CallNewGetKeyboard(UsernameRectangle userRec, bool CheckButtonPressed, string tokenUserName, string messageBoxTitle, string messageBoxDescription, string messageBoxText, bool isPassword)
    {
        //I took out ==true because it's redundant
        if ((userRec.Intersects(CursorRectangle)) && (CheckButtonPressed)) 
        {
            //and return the data without assigning it
            return await NewGetKeyboard(tokenUsername, messageBoxTitle, messageBoxDescription, messageBoxText, isPassword);  
        }
    }