c# - 无法将类型“System.Threading.Tasks.Task”隐式转换为“字符串”
问题描述
我想从 ShowKeyboard(...) 中的变量“inputtext”中获取字符串,但我不知道该怎么做。我总是在这行代码中收到一条错误消息:
KeyboardTextUsername = NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false);
错误 CS0029:无法将类型“System.Threading.Tasks.Task”隐式转换为“字符串”
编辑:更改代码后,我收到此错误消息:
KeyboardTextUsername = await NewGetKeyboard(Tokenusername, "Registration", "Username", "Choose a username", false);
错误 CS4033:“等待”运算符只能在异步方法中使用。考虑使用“异步”修饰符标记此方法并将其返回类型更改为“任务”。
我究竟做错了什么?我不知道如何解决这个问题。
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;
}
解决方案
您不能只在这样的方法之外调用 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);
}
}
推荐阅读
- ruby-on-rails - 如何获取引发错误的类
- python - Django annotate related field
- javascript - React 静态网站将除主页外的每个页面重定向到 404
- javascript - 在 KonvaJS 中水平和垂直居中文本
- c# - ODATA 4 如何获取当前用户?
- javascript - 将原生 redux 与 flatlist 反应,以在本地进行一些无限滚动
- angular - 构造函数中的变量初始化速度不够快,无法防止 htmlcode [Angular] 中的空指针异常
- reactjs - React Navigation Invariant 违规:尝试获取本机标签
- python - 在字典中查找重复值并仅在具有相同键的值不同时打印它们
- node.js - 无法将数据从 React Js 发布到 Node js