StreamedFileDataRequestedHandler Delegasikan

Definisi

Mewakili metode yang akan mengalirkan data ke StorageFile saat file tersebut pertama kali diakses.

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)

Parameter

stream
StreamedFileDataRequest

Permintaan untuk data yang dialirkan di StorageFile yang dibuat oleh metode CreateStreamedFileAsync atau ReplaceWithStreamedFileAsync .

Atribut

Persyaratan Windows

Rangkaian perangkat
Windows 10 (diperkenalkan dalam 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (diperkenalkan dalam v1.0)

Contoh

Contoh sederhana berikut menunjukkan cara membuat instans delegasi StreamedFileDataRequestedHandler. Contoh ini juga menunjukkan bagaimana instans ini menghasilkan aliran data untuk StorageFile yang dikembalikan oleh CreateStreamedFileAsync saat file pertama kali diakses.

Contoh sederhana ini hanya dimaksudkan untuk menunjukkan pola pemrograman untuk menggunakan delegasi StreamedFileDataRequestedHandler. Untuk file kecil dengan data yang mudah dihasilkan, pola streaming tidak berguna.

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);
    }
}

Keterangan

Instans delegasi yang Anda buat akan menghasilkan aliran data untuk StorageFile yang dikembalikan oleh metode CreateStreamedFileAsync atau ReplaceWithStreamedFileAsync .

Saat Anda menggunakan metode CreateStreamedFileAsync atau ReplaceWithStreamedFileAsync bersama dengan instans StreamedFileDataRequestedHandler, Anda dapat memberikan data untuk file sesuai permintaan alih-alih menulis konten file saat Anda membuatnya. Artinya, Anda dapat menangguhkan operasi mahal untuk menghasilkan data untuk file sampai file pertama kali diakses.

Berlaku untuk