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

1.1k views Asked by At

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:

<UserControl x:Class="WebPageOemViewer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" 
             d:DesignWidth="300">
    <Grid x:Name="MainZone">

    </Grid>
</UserControl>

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:

<Window x:Class="FillUserInfoWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:lcmb="clr-namespace:FClassroom.Presentation.Controls.MessageBoxes"
        Title="填写基础信息"
        WindowStartupLocation="CenterScreen"
        Width="770"
        Height="530"
        Loaded="FillUserInfoWindow_Loaded">
    <Grid x:Name="MainClient">

    </Grid>
</Window>

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.

1

There are 1 answers

0
Allen-Yao On BEST ANSWER

Finally, I got the answer from https://github.com/cefsharp/CefSharp/issues/1080#issuecomment-111969127

I am sorry post the duplicate thread online.

With the amaitland's help. I resolved this issue. We just needed remove OnMouseLeftButtonDown method.

Thanks for your time.