Back to Devexpress

AsyncDownloadPolicy.Downloading Event

corelibraries-devexpress-dot-data-dot-asyncdownloadpolicy-9e28c637.md

latest6.6 KB
Original Source

AsyncDownloadPolicy.Downloading Event

Allows you to spot an initiated download, check connection parameters, and cancel (or allow, if the SuppressAll() policy is active) the operation.

Namespace : DevExpress.Data

Assembly : DevExpress.Data.v25.2.dll

NuGet Package : DevExpress.Data

Declaration

csharp
public static event AsyncDownloadPolicy.WeakEventHandler<AsyncDownloadPolicy.DownloadingEventArgs> Downloading
vb
Public Shared Event Downloading As AsyncDownloadPolicy.WeakEventHandler(Of AsyncDownloadPolicy.DownloadingEventArgs)

Event Data

The Downloading event's data class is AsyncDownloadPolicy.DownloadingEventArgs. The following properties provide information specific to this event:

PropertyDescription
CancelGets or sets a value indicating whether the event should be canceled. Inherited from CancelEventArgs.
IsTrustedUriGets whether the processed resource is in a “safe” resource whitelist. Inherited from AsyncDownloadPolicy.AsyncDownloaderCancelEventArgs.
MethodGets the HTTP method.
ThrowOnNonTrustedUriGets or sets whether to throw an exception for unwanted download requests. Inherited from AsyncDownloadPolicy.AsyncDownloaderCancelEventArgs.
UriGets the processed resource identifier. Inherited from AsyncDownloadPolicy.AsyncDownloaderCancelEventArgs.
ValueTypeGets a value that identifies the group of controls to which an individual control (that initiated the download) belongs.

Remarks

csharp
static void Main() {
    // ...
    DevExpress.Data.AsyncDownloadPolicy.Downloading += AsyncDownloadPolicy_Downloading;
    Application.Run(new Form1());
}
private static void AsyncDownloadPolicy_Downloading(object sender, DevExpress.Data.AsyncDownloadPolicy.DownloadingEventArgs e) {
    e.Cancel = !e.Uri.AbsoluteUri.StartsWith(@"https://www.devexpress.com");
}
vb
Shared Sub Main()
    ' ...
    AddHandler DevExpress.Data.AsyncDownloadPolicy.Downloading, AddressOf AsyncDownloadPolicy_Downloading
    Application.Run(New Form1())
End Sub
Private Shared Sub AsyncDownloadPolicy_Downloading(ByVal sender As Object, ByVal e As DevExpress.Data.AsyncDownloadPolicy.DownloadingEventArgs)
    e.Cancel = Not e.Uri.AbsoluteUri.StartsWith("https://www.devexpress.com")
End Sub

Different DevExpress UI components with the same functionality (for example, separate WinForms PictureEdit controls that load images from URI paths) utilize the same HttpClient. As a result, the event’s e.ValueType parameter is the same for all individual controls (for aforementioned PictureEdit controls, e.ValueType returns ImageOrSvgImageResult). This technique allows you to identify the group of controls to which an individual control (that initiated the download) belongs.

csharp
public Form1() {
    InitializeComponent();
    AsyncDownloadPolicy.Downloading += AsyncDownloadPolicy_Downloading;
}
Dictionary<int, string> whitelist = new Dictionary<int, string>() {
    { 0, "www.devexpress.com" },
    { 1, "community.devexpress.com" },
    { 2, "docs.devexpress.com" },
    { 3, "supportcenter.devexpress.com" }
};
private void AsyncDownloadPolicy_Downloading(object sender, AsyncDownloadPolicy.DownloadingEventArgs e) {
    string result = e.ValueType.Name;
    // Checks whether a picture editor initiated the download.
    if(result == "ImageOrSvgImageResult")
        // Cancels downloading from domains that are not in the white list.
        e.Cancel = !whitelist.Values.Contains(e.Uri.Authority);
}
private void loadImageButton_Click(object sender, EventArgs e) {
    pictureEdit1.LoadAsync(/* YOUR_URL */);
}
vb
Public Sub New()
    InitializeComponent()
    AddHandler AsyncDownloadPolicy.Downloading, AddressOf AsyncDownloadPolicy_Downloading
End Sub
Private whitelist As New Dictionary(Of Integer, String)() From {
    { 0, "www.devexpress.com" },
    { 1, "community.devexpress.com" },
    { 2, "docs.devexpress.com" },
    { 3, "supportcenter.devexpress.com" }
}
Private Sub AsyncDownloadPolicy_Downloading(ByVal sender As Object, ByVal e As AsyncDownloadPolicy.DownloadingEventArgs)
    Dim result As String = e.ValueType.Name
    ' Checks whether a picture editor initiated the download.
    If result = "ImageOrSvgImageResult" Then
        ' Cancels downloading from domains that are not in the white list.
        e.Cancel = Not whitelist.Values.Contains(e.Uri.Authority)
    End If
End Sub
Private Sub loadImageButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    pictureEdit1.LoadAsync("YOUR_URL")
End Sub

Read the following topic for detailed information: Suppress Control Requests to Download Data from External URLs.

See Also

Downloaded

AsyncDownloadPolicy Class

AsyncDownloadPolicy Members

DevExpress.Data Namespace