【发布时间】:2020-05-24 18:44:33
【问题描述】:
我有DependencyProperty 的StorageFolder 类型:
public StorageFolder FolderForVideos
{
get { return (StorageFolder)GetValue(FolderForVideosProperty); }
set { SetValue(FolderForVideosProperty, value); }
}
public static readonly DependencyProperty FolderForVideosProperty =
DependencyProperty.Register("FolderForVideos", typeof(StorageFolder), typeof(MyControl), new PropertyMetadata(null);
我需要 FolderForVideo 的默认值作为视频保存文件夹:
StorageFolder folder= (await StorageLibrary.GetLibraryAsync(KnownLibraryId.Videos)).SaveFolder;
等待是这里的问题。因为显然我不能使用类似的东西:
public static readonly DependencyProperty FolderForVideosProperty =
DependencyProperty.Register("FolderForVideos", typeof(StorageFolder), typeof(MyControl), new
PropertyMetadata((await StorageLibrary.GetLibraryAsync(KnownLibraryId.Videos)).SaveFolder));
因为Error CS1992 The 'await' operator can only be used when contained within a method or lambda expression marked with the 'async' modifier FrameByFramePlayer C:\Users\toted\Desktop\Repos\videodetpl\FrameByFramePlayer\Custom\CameraControl.xaml.cs 110 Active
如何从异步操作中设置依赖属性的默认值?
【问题讨论】:
-
属性在语义上与字段相同,不应该是异步的。如果需要支持异步,请使用 getter / setter 方法。
标签: c# async-await dependency-properties