corelibraries-devexpress-dot-data-dot-asyncdownloadpolicy-9e28c637.md
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
public static event AsyncDownloadPolicy.WeakEventHandler<AsyncDownloadPolicy.DownloadingEventArgs> Downloading
Public Shared Event Downloading As AsyncDownloadPolicy.WeakEventHandler(Of AsyncDownloadPolicy.DownloadingEventArgs)
The Downloading event's data class is AsyncDownloadPolicy.DownloadingEventArgs. The following properties provide information specific to this event:
| Property | Description |
|---|---|
| Cancel | Gets or sets a value indicating whether the event should be canceled. Inherited from CancelEventArgs. |
| IsTrustedUri | Gets whether the processed resource is in a “safe” resource whitelist. Inherited from AsyncDownloadPolicy.AsyncDownloaderCancelEventArgs. |
| Method | Gets the HTTP method. |
| ThrowOnNonTrustedUri | Gets or sets whether to throw an exception for unwanted download requests. Inherited from AsyncDownloadPolicy.AsyncDownloaderCancelEventArgs. |
| Uri | Gets the processed resource identifier. Inherited from AsyncDownloadPolicy.AsyncDownloaderCancelEventArgs. |
| ValueType | Gets a value that identifies the group of controls to which an individual control (that initiated the download) belongs. |
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");
}
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.
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 */);
}
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