簡體   English   中英

CefSharp忽略通過RequestContext(Spellcheck)設置的首選項

[英]CefSharp ignores Preferences set with RequestContext (Spellcheck)

我嘗試為CefSharp Chromium嵌入式瀏覽器(安裝了NuGet的v3.3396.1786)和CefSharp.WPF組件(v67)啟用拼寫檢查。 我可以使用一種語言進行拼寫檢查,但是無法在運行時更改字典以進行拼寫檢查。 我嘗試了在CefSharps github頁面上顯示並鏈接的示例,但沒有成功。

無論我用RequestContext.SetPreference()設置了什么,我的CefSharp瀏覽器始終使用語言環境來確定用於拼寫檢查的語言。

這是我初始化Cef的代碼:

public static void Initialize()
{
    var settings = new CefSettings
    {
        BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
            Environment.Is64BitProcess ? "x64" : "x86",
            "CefSharp.BrowserSubprocess.exe"),
        Locale = "de-DE",
        RemoteDebuggingPort = 8088,
    };

    // Set BrowserSubProcessPath based on app bitness at runtime

    // Make sure you set performDependencyCheck false
    Cef.Initialize
    (
        settings,
        performDependencyCheck: false,
        browserProcessHandler: null
    );
    CefSharpSettings.LegacyJavascriptBindingEnabled = true;
}

實際的瀏覽器是通過另一種方法設置和創建的:

private void create_web_browser()
{
    current_web_browser = new ChromiumWebBrowser
    {
        Visibility = Visibility.Hidden,
        BrowserSettings = new BrowserSettings
        {
            FileAccessFromFileUrls = CefState.Enabled,
            UniversalAccessFromFileUrls = CefState.Enabled,
            Javascript = CefState.Enabled,
            ImageLoading = CefState.Enabled,
            JavascriptAccessClipboard = CefState.Enabled,
            JavascriptCloseWindows = CefState.Enabled,
            JavascriptDomPaste = CefState.Enabled
        }           
    };

    current_helper = new ChromiumObjectForScriptingHelper(web_browser_ready_async, current_web_browser);
    if (ToolbarConfig != null)
    {
        current_helper.SetToolbarConfig(ToolbarConfig);
    }
    current_web_browser.RegisterJsObject("callbackObj", current_helper);
    var cur_dir = Directory.GetCurrentDirectory();
    var url = $"file://{cur_dir}/ckeditor/editor.html";
    current_web_browser.Address = url;
    current_web_browser.RequestContext = new RequestContext();
    current_web_browser.RequestContext.SetPreference("browser.enable_spellchecking", true, out _);
    current_web_browser.RequestContext.SetPreference("spellcheck.dictionaries", new List<string> { "en-US" }, out _);
    grid.Children.Add(current_web_browser);
}

另一種方法用於使用戶以后更改語言:

public void SetSpellcheck(Spellcheck language)
{
    if (language == Spellcheck.None) return;
    current_web_browser.RequestContext.SetPreference("spellcheck.dictionaries", new List<string> { get_locale_for_language(language) }, out _);
}

如您所見,我嘗試設置拼寫檢查設置,但是無論在那里設置什么,它都無效。 我可以將enable_spellcheck設置為false,它仍然檢查拼寫,並且我設置的字典也將被忽略。 代替我在詞典中輸入的內容,將使用先前在“ Locale設置的Locale (我檢查了out變量,但是沒有錯誤)我也嘗試使用全局RequestContext,但是沒有成功。

顯然其他人以某種方式使它起作用,所以我感覺好像我在這里錯過了一些重要的事情,或者做的事情完全愚蠢。

另一件事是,如果我使用GetAllPreferences(true)來獲取具有默認設置的所有設置的列表,我只會得到null

感謝@amaitlands的評論,我現在知道問題是我在錯誤的線程中設置了首選項。 我誤以為CefSharp實際上是在自己的應用程序中運行在我的應用程序UI-Thread中。

解決方案是使用Cef.UIThreadTaskFactory.StartNew()在CefSharp UI-Thread內部運行代碼

Cef.UIThreadTaskFactory.StartNew(delegate
{
    current_web_browser.RequestContext.SetPreference("browser.enable_spellchecking", true, out _);
    current_web_browser.RequestContext.SetPreference("spellcheck.dictionaries", new List<object> { "en-US" }, out _);
});

我還必須將List<>的類型更改為object因為我使用的是舊版本的CefSharp.WPF,否則我會Trying to set a preference of type LIST to value of type NULL錯誤Trying to set a preference of type LIST to value of type NULL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM