Dela via


StreamedFileDataRequestedHandler Delegate

Definition

Represents the method that will stream data into a StorageFile when that file is first accessed.

public delegate void StreamedFileDataRequestedHandler(StreamedFileDataRequest ^ stream);
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(4277577764, 12257, 19719, 163, 91, 183, 124, 80, 181, 244, 204)]
class StreamedFileDataRequestedHandler : MulticastDelegate
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Guid(4277577764, 12257, 19719, 163, 91, 183, 124, 80, 181, 244, 204)]
public delegate void StreamedFileDataRequestedHandler(StreamedFileDataRequest stream);
var streamedFileDataRequestedHandlerHandler = function(stream){
/* Your code */
}
Public Delegate Sub StreamedFileDataRequestedHandler(stream As StreamedFileDataRequest)

Parameters

stream
StreamedFileDataRequest

The request for the streamed data in the StorageFile that was created by the CreateStreamedFileAsync or ReplaceWithStreamedFileAsync method.

Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

The following simple example demonstrates how to create an instance of the StreamedFileDataRequestedHandler delegate. The example also shows how this instance produces the data stream for the StorageFile that was returned by the CreateStreamedFileAsync when the file is first accessed.

This simple example is intended only to demonstrate the programming pattern for using the StreamedFileDataRequestedHandler delegate. For a small file with data that's easy to produce, the streaming pattern is not useful.

using Windows.Storage;
using Windows.Storage.Streams;

private async void CreateStreamedFile()
{
    // Create a streamed file.
    StorageFile file =
    await StorageFile.CreateStreamedFileAsync("file.txt",
        StreamedFileWriter, null);

    // Prepare to copy the file.
    StorageFolder localFolder = ApplicationData.Current.LocalFolder;
    string newName = "copied_file.txt";

    // Copy the streamed file. At this point,
    // the data is streamed into the source file.
    await file.CopyAsync(localFolder, newName,
        NameCollisionOption.ReplaceExisting);
}

private async void StreamedFileWriter(StreamedFileDataRequest request)
{
    try
    {
        using (var stream = request.AsStreamForWrite())
        using (var streamWriter = new StreamWriter(stream))
        {
            for (int l = 0; l < 50; l++)
            {
                await streamWriter.WriteLineAsync("Data.");
            }
        }
        request.Dispose();
    }
    catch (Exception ex)
    {
        request.FailAndClose(StreamedFileFailureMode.Incomplete);
    }
}

Remarks

The instance of the delegate that you create will produce the data stream for the StorageFile that was returned by the CreateStreamedFileAsync or ReplaceWithStreamedFileAsync method.

When you use the CreateStreamedFileAsync or ReplaceWithStreamedFileAsync method together with an instance of StreamedFileDataRequestedHandler, you can provide the data for a file on demand instead of writing the contents of a file when you create it. That is, you can defer expensive operations to produce the data for the file until the file is first accessed.

Applies to