WPF中对于异步返回的绑定

举个例子,实现一个用户头像的Image控件,绑定的是url。而我的需求是下载url的图片,做本地缓存,再显示。

Converter实现需要继承自IValueConverter,而它的两个接口都是同步,而下载函数是异步async的。此时在Convert函数实现内使用await关键词是不允许的。

解决方案思路:

返回一个新的类对象,该类包装Task,继承自INotifyPropertyChanged,带一属性Result。当Task完成的时候,给Result赋值,同时触发INotifyPropertyChanged中的事件。

XAML中绑定对象的控件需要将原来的Content绑定设置到DataContext上,Content另绑定Converter返回的对象的Result属性。

上一点代码:

    public sealed class TaskCompletionNotifier<TResult> : INotifyPropertyChanged
    {
        public TaskCompletionNotifier(Task<TResult> task)
        {
            Task = task;
            if (!task.IsCompleted)
            {
                var scheduler = (SynchronizationContext.Current == null) ? TaskScheduler.Current : TaskScheduler.FromCurrentSynchronizationContext();
                task.ContinueWith(t =>
                {
                    var propertyChanged = PropertyChanged;
                    if (propertyChanged != null)
                    {
                        propertyChanged(this, new PropertyChangedEventArgs("IsCompleted"));
                        if (t.IsCompleted)
                        {
                            propertyChanged(this, new PropertyChangedEventArgs("Result"));
                        }else{//do something else;
                        }
                    }
                },
                CancellationToken.None,
                TaskContinuationOptions.ExecuteSynchronously,
                scheduler);
            }
        }
        public Task<TResult> Task { get; private set; }
        public TResult Result { get { return (Task.Status == TaskStatus.RanToCompletion) ? Task.Result : default(TResult); } }
        public event PropertyChangedEventHandler PropertyChanged;
    }

 

更多细节查看:https://docs.microsoft.com/en-us/archive/msdn-magazine/2014/march/async-programming-patterns-for-asynchronous-mvvm-applications-data-binding

 

posted @ 2021-08-10 16:47  陈惊蛰  阅读(304)  评论(0编辑  收藏  举报