简体   繁体   English

如何使用 CefSharp 实现 DownloadHandler 接口

[英]How to implament DownloadHandler interface with CefSharp

i wrote a Custom Download Handler Class in Visual studio and have completed the implamentation of the download handler but when i look at the errors it says that i need to implament a interface for MyCustomDownloadHandler and i dont know how but i have the actual interface code given to me in visual studio but i dont know what to put in the brackets我在 Visual Studio 中编写了一个自定义下载处理程序类,并完成了下载处理程序的实现,但是当我查看错误时,它说我需要为 MyCustomDownloadHandler 实现一个接口,我不知道如何实现,但我已经给出了实际的接口代码在 visual studio 中对我来说,但我不知道要在括号中放什么

    public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, string requestMethod)
    {
        //i dont know what to enter here.
    }

here is the MYCustomDownloadHandler Class Code:这是 MYCustomDownloadHandler 类代码:

    public event EventHandler<DownloadItem> OnBeforeDownloadFired;

    public event EventHandler<DownloadItem> OnDownloadUpdatedFired;

    public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, string requestMethod)
    {
        //What do i enter here.
    }

    public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
    {
        if (downloadItem.IsValid)
        {
            Console.WriteLine("== File information ========================");
            Console.WriteLine(" File URL: {0}", downloadItem.Url);
            Console.WriteLine(" Suggested FileName: {0}", downloadItem.SuggestedFileName);
            Console.WriteLine(" MimeType: {0}", downloadItem.MimeType);
            Console.WriteLine(" Content Disposition: {0}", downloadItem.ContentDisposition);
            Console.WriteLine(" Total Size: {0}", downloadItem.TotalBytes);
            Console.WriteLine("============================================");
        }

        OnBeforeDownloadFired?.Invoke(this, downloadItem);

        if (!callback.IsDisposed)
        {
            using (callback)
            {
                callback.Continue(
                    downloadItem.SuggestedFileName,
                    showDialog: true
                );
            }
        }
    }

    /// https://cefsharp.github.io/api/51.0.0/html/T_CefSharp_DownloadItem.htm
    public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
    {
        OnDownloadUpdatedFired?.Invoke(this, downloadItem);

        if (downloadItem.IsValid)
        {
            // Show progress of the download
            if (downloadItem.IsInProgress && (downloadItem.PercentComplete != 0))
            {
                Console.WriteLine(
                    "Current Download Speed: {0} bytes ({1}%)",
                    downloadItem.CurrentSpeed,
                    downloadItem.PercentComplete
                );
            }

            if (downloadItem.IsComplete)
            {
                Console.WriteLine("The download has been finished !");
            }
        }
    }
}

i couldent fit the namespace in the code, please forgive me i am farly new to formatting.我无法在代码中放入命名空间,请原谅我对格式化还很陌生。

Return true to proceed with the download or false to cancel the download.返回 true 继续下载或返回 false 取消下载。

If you wish to allow the download then return true;如果您希望允许下载,则返回 true;

public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, string requestMethod)
{
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM