CefSharp。Wpf WebView不能接受输入,链接点击没有响应

时间:2022-08-11 21:22:03

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

我用CefSharp。Wpf应用程序。版本: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.

我通过CefSharp WebView托管这个网页。我可以把我的网页放在里面。但是WebView中的文本框不能接受输入,并且超链接不能响应。

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

这是我的代码。我创建了一个名为WebPageOemViewer的Wpf Usercontrol。

Xaml:

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.

网页将显示在窗口中。但是搜索框和超链接没有反应。我还尝试了最新的CefSharp.Wpf。都不能工作。

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 个解决方案

#1


0  

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

最后,我从https://github.com/cefsharp/cefsharp/cefsharp/issues /1080#issue -111969127得到了答案

I am sorry post the duplicate thread online.

很抱歉,我把复制的帖子放到网上了。

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

amaitland的帮助。我解决了这个问题。我们只需要删除OnMouseLeftButtonDown方法。

Thanks for your time.

谢谢你的时间。

#1


0  

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

最后,我从https://github.com/cefsharp/cefsharp/cefsharp/issues /1080#issue -111969127得到了答案

I am sorry post the duplicate thread online.

很抱歉,我把复制的帖子放到网上了。

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

amaitland的帮助。我解决了这个问题。我们只需要删除OnMouseLeftButtonDown方法。

Thanks for your time.

谢谢你的时间。