【发布时间】:2019-03-30 00:05:25
【问题描述】:
在我的 FolderViewModel 中,我有
public string FolderPath
{
get
{
if (folderPath == null)
{
GetFolderPathAsync();
return "Loading...";
}
return folderPath;
}
set
{
folderPath = value;
Changed(nameof(FolderPath));
}
}
GetFolderPathAsync 是一种异步方法,它使服务器调用以获取路径并设置 FolderPath。
现在在另一个类中,我创建文件夹视图模型并以这种方式设置它们的路径
folderViewModel.FolderPath = parent.FolderPath+"/"+folder.Name;
问题在于,路径最终设置为“正在加载.../文件夹名称”,并且当父文件夹的文件夹路径在从服务器获取后从“正在加载...”更新时永远不会更新。我该如何解决?我不擅长线程,所以我真的不知道如何解决这个问题。我想知道是否有办法让 folderPath 的设置等待 GetFolderPathAsync 以某种方式完成?
感谢您的帮助!
【问题讨论】:
-
属性不应启动异步操作。如果您从异步方法调用
GetFolderPathAsync方法,您可以等待它,然后在完成后将数据绑定属性设置为“正在加载”。这假设GetFolderPathAsync返回一个Task。 -
您可以简单地在
GetFolderPathAsync()之后添加一个继续运行的任务来调用和发出通知。
标签: c# wpf multithreading mvvm