Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更新异步获取的viewmodel属性?_C#_Wpf_Multithreading_Mvvm - Fatal编程技术网

C# 如何更新异步获取的viewmodel属性?

C# 如何更新异步获取的viewmodel属性?,c#,wpf,multithreading,mvvm,C#,Wpf,Multithreading,Mvvm,在我的FolderViewModel中,我有 public string FolderPath { get { if (folderPath == null) { GetFolderPathAsync(); return "Loading..."; }

在我的FolderViewModel中,我有

public string FolderPath
        {
            get
            {
                if (folderPath == null)
                {
                    GetFolderPathAsync();
                    return "Loading...";
                }
                return folderPath;
            }
            set
            {
                folderPath = value;
                Changed(nameof(FolderPath));
            }
        }
GetFolderPathAsync是一种异步方法,它进行服务器调用以获取路径并设置FolderPath

现在在另一个类中,我创建folderviewmodels,并以这种方式设置它们的路径

folderViewModel.FolderPath = parent.FolderPath+"/"+folder.Name;
问题在于,路径最终被设置为“Loading…/foldername”,并且当父文件夹的folderpath从服务器获取后从“Loading…”更新时,该路径从不更新。我怎样才能修好它?我不擅长线程,所以我真的不知道如何解决这个问题。我想知道是否有办法让folderPath的设置等待GetFolderPathAsync以某种方式完成


谢谢你的帮助

属性不应启动异步操作。这就是C#中不支持
async
属性的主要原因。有关更多信息,请参阅

如果从
async
方法调用
GetFolderPathAsync
方法,则可以
等待它,然后在完成后将数据绑定属性设置为“加载…”。这假设
GetFolderPathAsync
返回一个
Task
或一个
Task

另一个选项是使用该方法创建在任务完成时异步执行的延续:

if (folderPath == null)
{
    GetFolderPathAsync().ContinueWith(_ => 
    {
        folderPath = "Loading...";
        Changed(nameof(FolderPath));
    });
    return folderPath;
}

属性不应启动异步操作。这就是C#中不支持
async
属性的主要原因。有关更多信息,请参阅

如果从
async
方法调用
GetFolderPathAsync
方法,则可以
等待它,然后在完成后将数据绑定属性设置为“加载…”。这假设
GetFolderPathAsync
返回一个
Task
或一个
Task

另一个选项是使用该方法创建在任务完成时异步执行的延续:

if (folderPath == null)
{
    GetFolderPathAsync().ContinueWith(_ => 
    {
        folderPath = "Loading...";
        Changed(nameof(FolderPath));
    });
    return folderPath;
}

属性的可能重复不应启动异步操作。如果从异步方法调用
GetFolderPathAsync
方法,可以等待它,然后在完成后将数据绑定属性设置为“加载”。这假设
GetFolderPathAsync
返回一个
任务
。您只需添加一个继续任务,在
GetFolderPathAsync()
之后运行,即可调用并引发通知。可能的属性重复不应启动异步操作。如果从异步方法调用
GetFolderPathAsync
方法,可以等待它,然后在完成后将数据绑定属性设置为“加载”。这假设
GetFolderPathAsync
返回一个
任务
。您只需添加一个继续任务,在
GetFolderPathAsync()
之后运行,即可调用并发出通知。@an007:哪个部分不起作用?同样,从属性的getter调用异步方法是错误的,因此您应该重新考虑您的设计。@an007:哪个部分不起作用?同样,从属性的getter调用异步方法是错误的,因此您应该重新考虑您的设计。