0

我已经在这个论坛上搜索了我的问题,但没有找到任何适合的东西,我的程序流程有问题。

我在 Azure 上有一个 MobileService,它有一个问题表,我的应用程序有一个主菜单和一个将用户带到测验页面的测验按钮,在测验页面上我有一个开始测验按钮,它显示列表中的第一个问题。

这是我用来从数据库中获取问题的代码,我把它放在页面构造函数中,现在当用户按下测验按钮时,页面打开有一个延迟,这还不错,因为它的等待时间不长,只有几秒钟,有没有更好的方法来做到这一点?

Task<IMobileServiceTable<Question>> getDataFromDatabase = new Task<IMobileServiceTable<Question>>(getQuestions);
getDataFromDatabase.Start();
QuestionList = await getDataFromDatabase;

在同一个函数中,我有这段代码修改了开始测验按钮的 isEnabled 属性。除非数据来自服务器,否则这会停止测验继续进行,但它不会一直工作,有时启动按钮已启用设置为 true,即使任务已完成,我也会从我的 MobileServiceCollectionView QuestionList 获得空引用。

Task<bool> assignData = new Task<bool>(assignTabletoitems);
assignData.Start();
startbutton.IsEnabled = await assignData;

对此的任何帮助将不胜感激。

谢谢

4

1 回答 1

0

You shouldn't have to create a new instance of Task<T> to query the database (you haven't provided the definition of getQuestions which is used in your first code snippet, so I can't tell whether that code is doing what it's supposed to). What you'd typically do is to get a table for the appropriate type from the MobileServiceClient object, and then query on it:

var client = new MobileServiceClient(appUrl, appKey);
var table = client.GetTable<Question>();
var questionList = await table.ToListAsync();

Regarding your second code snippet, without the definition of assignTabletoitems it's really hard to know what you're intending that code to do.

于 2013-04-01T19:46:17.850 回答