I have DependencyProperty
of StorageFolder
type:
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);
I need default value for FolderForVideo
to be videos save folder:
StorageFolder folder= (await StorageLibrary.GetLibraryAsync(KnownLibraryId.Videos)).SaveFolder;
The await is the issue here. Because obviously I can't use something like:
public static readonly DependencyProperty FolderForVideosProperty =
DependencyProperty.Register("FolderForVideos", typeof(StorageFolder), typeof(MyControl), new
PropertyMetadata((await StorageLibrary.GetLibraryAsync(KnownLibraryId.Videos)).SaveFolder));
because of 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
How to set default value for dependency property from async operation?
Let me share my thoughts about this problem.
Let's forget the DependencyProperty
for a minute. What you want to achieve is to call an async function during static object initialization.
static ComplexObject co = new ComplexObject(await GetSomeValueAsync());
Generally speaking allocating space for an object should be quick and as error-free as possible. Error should be thrown only if allocation is refused for whatever reason.
Let's so suppose you are calling your async code in sync (Please don't do that, it is just for demonstrational purpose):
static ComplexObject co = new ComplexObject(GetSomeValueAsync().GetAwaiter().GetResult());
What if your I/O related operation fails? What if it took several minutes to get a response? In either case you break the promise of the object allocation.
I would suggest to consider alternative ways to inject the default folder:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.