Skip to content

CefSharp.Wpf WebView cannot accept input and the link clicked no response #1080

@Allen-Yao

Description

@Allen-Yao

Hi,

I used CefSharp.Wpf in my application. version: 1.25.7.

I hosted the webpage via CefSharp WebView. I can host my webpage in it. But the TextBox in the WebView cannot accept the input, and the hyperlink cannot response.

Here is my code. I created a Wpf Usercontrol named WebPageOemViewer.

Xaml:


</Grid>

Code behind:

public partial class WebPageOemViewer : UserControl, IRequestHandler
{
private WebView browser;

    public WebPageOemViewer(string url)
    {
        InitializeComponent();

        CEF.Initialize(new Settings { LogSeverity = LogSeverity.Disable, PackLoadingDisabled = true });

        var browserSettings = new BrowserSettings { ApplicationCacheDisabled = true, PageCacheDisabled = true };

        browser = new WebView(string.Empty, browserSettings)
        {
            Address = url,
            RequestHandler = this,
            Background = Brushes.White
        };

        browser.LoadCompleted += Browser_LoadCompleted;

        CEFJsObjectProcessor processor = new CEFJsObjectProcessor();

        browser.RegisterJsObject("CEFJsObject", processor);

        MainZone.Children.Insert(0, browser);
    }

    public void View(string url)
    {
        if (browser.IsBrowserInitialized)
        {
            browser.Visibility = Visibility.Hidden;

            browser.Load(url);
        }
    }

    public void Close()
    {
        browser.Dispose();

        MainZone.Children.Remove(browser);
    }

    public static void Shutdown()
    {
        Thread.Sleep(1000);

        if (CEF.IsInitialized)
        {
            CEF.Shutdown();
        }
    }

    private void Browser_LoadCompleted(object sender, LoadCompletedEventArgs e)
    {
        Dispatcher.Invoke(new Action(() =>
        {
            browser.Visibility = Visibility.Visible;
        }));
    }

    #region IRequestHandler

    public bool GetAuthCredentials(IWebBrowser browser, bool isProxy, string host, int port, string realm, string scheme, ref string username, ref string password)
    {
        return false;
    }

    public bool GetDownloadHandler(IWebBrowser browser, string mimeType, string fileName, long contentLength, ref IDownloadHandler handler)
    {
        return true;
    }

    public bool OnBeforeBrowse(IWebBrowser browser, IRequest request, NavigationType naigationvType, bool isRedirect)
    {
        return false;
    }

    public bool OnBeforeResourceLoad(IWebBrowser browser, IRequestResponse requestResponse)
    {
        var headers = requestResponse.Request.GetHeaders();

        headers[Constants.Common.HttpHeaderFromKey] = Constants.Common.HttpHeaderFromValue;

        requestResponse.Request.SetHeaders(headers);

        return false;
    }

    public void OnResourceResponse(IWebBrowser browser, string url, int status, string statusText, string mimeType, System.Net.WebHeaderCollection headers)
    {

    }

    #endregion

    internal class CEFJsObjectProcessor
    {
        public event Action<string> Completed;

        private void CompletedInfo(string parameter)
        {
            if (Completed != null)
            {
                Completed(parameter);
            }
        }
    }
}

I host the Usercontrol in a Window named FillUserInfoWindow

Xaml:


</Grid>

Code behind:

public partial class FillUserInfoWindow : Window
{
private WebPageOemViewer _viewer;

    private string _url = string.Empty;

    public FillUserInfoWindow()
    {
        InitializeComponent();
    }

    public FillUserInfoWindow(string url) 
    {
        _url = url;

        InitializeComponent();
    }

    private void FillUserInfoWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Button CloseButton = this.Template.FindName("CloseButton", this) as Button;

        CloseButton.Click += CloseButton_Click;

        if(_viewer != null)
        {
            _viewer.Visibility = Visibility.Visible;
        }

        LoadWebPage(_url);
    }

    private void CloseButton_Click(object sender, RoutedEventArgs e)
    {
        Shutdown();

        Close();
    }

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        DragMove();

        base.OnMouseLeftButtonDown(e);
    }

    private void LoadWebPage(string url)
    {
        Application.Current.Dispatcher.Invoke(new Action(() => {

            if (_viewer == null)
            {
                _viewer = new WebPageOemViewer(url);

                MainClient.Children.Insert(0, _viewer);
            }

            _viewer.Visibility = Visibility.Visible;

            _viewer.View(url);

        }));

    }

    public void Shutdown()
    {
        if(_viewer != null)
        {
            _viewer.Close();
        }
    }
}

The webpage will shown in the Window. But the Search Box and hyperlinks no response. Also I tried the latest CefSharp.Wpf. Both cannot work.

string address = "http://www.bing.com";

FillUserInfoWindow fills = new FillUserInfoWindow(address);

fills.ShowDialog();

You can reproduce this issue by the code above. Thanks for your time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    cef1wpfWPF Implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions