无法将类型 'System.Threading.Tasks.Task' 隐式转换为 'string'
Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'string'
我想从 ShowKeyboard(...) 中的变量 "inputtext" 获取字符串,但我不知道该怎么做。我总是在这行代码中收到错误消息:
KeyboardTextUsername = NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false);
Error CS0029: Cannot implicitly convert type
'System.Threading.Tasks.Task' to 'string'
编辑:更改代码后,我收到此错误消息:
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'.
我做错了什么?我不知道如何解决这个问题。
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;
}
NewGetKeyboard
是一个 async
方法,应该使用 await
关键字
调用它
protected void async MyButtonClickHandler(object sender, EventArgs args)
{
...
KeyboardTextUsername = await NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false);
...
}
您不能只在这样的方法之外调用 await,这也适用于您的条件。 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;
}
}
这是上面代码的一个简短版本..
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);
}
}
如果你不想让它异步
KeyboardTextUsername = NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false).GetAwaiter().GetResult();
我想从 ShowKeyboard(...) 中的变量 "inputtext" 获取字符串,但我不知道该怎么做。我总是在这行代码中收到错误消息:
KeyboardTextUsername = NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false);
Error CS0029: Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'string'
编辑:更改代码后,我收到此错误消息:
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'.
我做错了什么?我不知道如何解决这个问题。
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;
}
NewGetKeyboard
是一个 async
方法,应该使用 await
关键字
protected void async MyButtonClickHandler(object sender, EventArgs args)
{
...
KeyboardTextUsername = await NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false);
...
}
您不能只在这样的方法之外调用 await,这也适用于您的条件。 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;
}
}
这是上面代码的一个简短版本..
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);
}
}
如果你不想让它异步
KeyboardTextUsername = NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false).GetAwaiter().GetResult();