1

我无法将异步数据绑定到文本块。

当我使用 F11 逐步完成我的代码时,它可以工作。但是当我只是运行我的应用程序时,它会抛出一个错误说:

Value cannot be null. 

看起来代码运行得太快了。

这是我的代码:

settings.GetSettings();
tbNamePlayer1.Text = settings.player1;
tbNamePlayer2.Text = settings.player2;

获取设置方法:

StorageFolder sf = await Package.Current.InstalledLocation.GetFolderAsync("XML");
StorageFile st;
try {
    st = await sf.GetFileAsync(filename);
} catch {
    WriteInitialSettings();
}
st = await sf.GetFileAsync(filename);
var reader = XmlReader.Create(st.Path);
XmlSerializer ser = new XmlSerializer(typeof(Settings));
settings = (Settings)ser.Deserialize(reader);
player1 = settings.player1;
player2 = settings.player2;
difficulty = settings.difficulty;
win = settings.win;
lose = settings.lose;
4

2 回答 2

1

你需要await你的GetSettings方法:

await settings.GetSettings();
tbNamePlayer1.Text = settings.player1;
tbNamePlayer2.Text = settings.player2;

这样,您的方法将(异步)等待直到GetSettings完成,然后再分配属性。

于 2012-11-27T13:52:59.373 回答
0

查看 xaml 中的 IsAsync 或/和优先级绑定。也许这可以帮助你

http://www.switchonthecode.com/tutorials/wpf-tutorial-priority-bindings

于 2012-11-27T10:11:35.200 回答