代码之家  ›  专栏  ›  技术社区  ›  Andrew_STOP_RU_WAR_IN_UA

Cef-上载不显示OpenFileDialog的文件

  •  0
  • Andrew_STOP_RU_WAR_IN_UA  · 技术社区  · 6 年前

    我需要有一种方法来选择一些不显示OpenFileDialog的文件。

    是的,我知道CEF不是实现自动化的最好方法,但是我需要用CEF来实现这一点。

    我发现从2014年开始这是可能的: https://github.com/cefsharp/CefSharp/pull/342/commits/c11fe8e4e97179ff4073208c13f9ff29e61bab79

    在此提交中,添加了重写文件浏览对话框结果的功能。。。但我还是不明白如何运用这种能力。。。

    我已经找到了使用示例,但它不起作用:

    using System.Collections.Generic;
    using System.IO;
    namespace CefSharp.Example
    {
        public class TempFileDialogHandler : IDialogHandler
        {
            public bool OnFileDialog(IWebBrowser browser, string title, string defaultFileName, List<string> acceptTypes, out List<string> result)
            {
                result = new List<string> { Path.GetRandomFileName() };
                return true;
            }
        }
    }
    

    现在OnFileDialog中的IDialogHandler有了另一个参数(没有结果),这是错误的。

    当前参数列表为:

    public bool OnFileDialog(IWebBrowser browserControl, IBrowser browser, CefFileDialogMode mode, string title, string defaultFilePath, List<string> acceptFilters, int selectedAcceptFilter, IFileDialogCallback callback)
    

    有人能帮我吗?

    我用的是最新的CEFsharp:63.0.3

    1 回复  |  直到 6 年前
        1
  •  2
  •   Andrew_STOP_RU_WAR_IN_UA    6 年前
    public class TempFileDialogHandler : IDialogHandler
    {
        string[] _filePath;
    
        public TempFileDialogHandler(params string[] filePath)
        {
            _filePath = filePath;
        }
    
        public bool OnFileDialog(IWebBrowser browserControl, IBrowser browser, CefFileDialogMode mode, string title, string defaultFilePath, List<string> acceptFilters, int selectedAcceptFilter, IFileDialogCallback callback)
        {
            callback.Continue(0, _filePath.ToList());
            return true;
        }
    }
    

    使用方法:

    Browser.DialogHandler = new TempFileDialogHandler(files);