If I write a simple function, I will immediately get the result. If I use async/awaitand return Task, the method will return when it is done with the task, but what if I need to write a method that needs to be returned immediately, but then continue updating the result and, possibly, ending the task? Also, if I want to expose it outside the WinRT component library for consumption in components in other languages? How can I do this in C # and how do I do this in C ++ / CX? Or is JS possible?
Example 1:
I want to show a property that returns ImageSource, so I can immediately associate it with my MVVM model in the XAML view. The loading method ImageSourcewill be in a separate class that is displayed outside the WinRT component (this is a public method). Now I want this method to be expected, or at least somehow return a task that I can expect, but also return immediately ImageSource, so the property that I call can return immediately, since the properties cannot be asynchronous . The caller does not know what type ImageSourcewill be, so he cannot create it, because it ImageSourceis actually an abstract type and is usually represented as BitmapImageorWriteableBitmap, and in my case both can be returned from the method. Obviously, the method itself immediately knows whether it will return one of the types of objects, but it takes some time to read / create and decode the image.
I think the signature may be something like this in C #
public async Task<ImageSource> GetImage(
object key,
out ImageSource bitmap,
CancellationToken cancellationToken)
and I simply would not expect the result of the method in the properties accessory, but I think that I can immediately return the bitmap argument, and when it is called in another place or event in another place in the code of my view model, I will wait or cancel the task.
Example 2:
I want to be able to list files from disk and get a task that will be completed when all files are specified, but will immediately return the IObservableVector view model representing files for use in my XAML user interface, which is updated as pages are loaded asynchronously.
, , :
public async Task<int> GetImages(
object queryParemeters,
out ObservableCollection<CustomFileInfoType> files,
CancellationToken cancellationToken)
, , TPL WinRT, Task WinRT, , , , , public, IAsyncOperation, AsyncInfo.Run(), . ObservableCollection .NET, , , , IObservableVector, , .NET. , , , .
- ++/CX? JS?