TargetFileRequestedEventArgs TargetFileRequestedEventArgs TargetFileRequestedEventArgs TargetFileRequestedEventArgs Class

Definition

Provides information about a targetfilerequested event.

public : sealed class TargetFileRequestedEventArgs : ITargetFileRequestedEventArgspublic sealed class TargetFileRequestedEventArgs : ITargetFileRequestedEventArgsPublic NotInheritable Class TargetFileRequestedEventArgs Implements ITargetFileRequestedEventArgs// You can use this class in JavaScript.
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

The File picker sample demonstrates how to respond to a targetfilerequested event.


// Event handler
private async void OnTargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs e)
{
    // Respond to TargetFileRequested event on the background thread on which it was raised

    // Requesting a deferral allows the app to call another asynchronous method and complete the request at a later time
    var deferral = e.Request.GetDeferral();

    // Create file and assign to TargetFile property
    e.Request.TargetFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(sender.FileName, CreationCollisionOption.GenerateUniqueName);

    // Complete the deferral to let the Picker know the request is finished
    deferral.Complete();
}

// Register for the event
fileSavePickerUI.TargetFileRequested += new TypedEventHandler<FileSavePickerUI, TargetFileRequestedEventArgs>(OnTargetFileRequested);

// Event handler
function onTargetFileRequested(e) {
    var deferral;
    deferral = e.request.getDeferral();

    // Create a file to pass to the file picker which then gives it to the calling app
    Windows.Storage.ApplicationData.current.localFolder.createFileAsync(fileSavePickerUI.fileName).done(function (file) {
            // Assign the resulting file to the targetFile property and complete the deferral to indicate success
        e.request.targetFile = file;
        deferral.complete();
    }, function () {
        // Set the targetFile property to null and complete the deferral to indicate failure
        e.request.targetFile = null;
        deferral.complete();
    });
};

// Register for the event
fileSavePickerUI.addEventListener("targetfilerequested", onTargetFileRequested, false);

In the example, e contains a TargetFileRequestedEventArgs object.

Remarks

This object is passed to the event handler for targetfilerequested events.

Responding to a targetfilerequested event

If your app participates in the File Save Picker contract and a targetfilerequested event fires, your app should respond by following these steps:

  1. Get a targetFileRequest using the targetFileRequestedEventArgs.request property.
  2. Create (or retrieve) a storageFile to represent the file to save; this storageFile is returned to the app that called the file picker to save and used by the calling app to write content to the file.

The file name and extension of the object that represents the file must match the file name and extension specified by the user (and accessible through FileName ) or the attempt to save the file will fail. If the attempt fails, the user can try to save the file again.

If your app (as the provider of the save location) can't provide an object for the file to save, set targetFileRequest.targetFile to null.

  1. Set targetFileRequest.targetFile to the storageFile object. ### Responding asynchronously

If your app, which is providing the save location, can't finish responding to the targetfilerequested event before it returns from its event handler (for example, if your app calls an asynchronous method), you can complete your response asynchronously by deferring.

Your app, as the provider of the save location, can defer in order to respond to the event asynchronously by following these steps:

  1. Get a targetFileRequest using the targetFileRequestedEventArgs.request property.
  2. Call targetFileRequest.getDeferral to get a targetFileRequestDeferral object.
  3. Perform the steps needed to respond to the targetfilerequested event (described in the preceding section).
  4. Call targetFileRequestDeferral.complete to signal that your app has finished responding to the targetfilerequested event.

Properties

Request Request Request Request

Gets a targetFileRequest object that is used to respond to a targetfilerequested event.

public : TargetFileRequest Request { get; }public TargetFileRequest Request { get; }Public ReadOnly Property Request As TargetFileRequest// You can use this property in JavaScript.
See Also

See Also