我在应用程序中使用了CefSharp.Wpf。 版本:1.25.7。
我通过CefSharp WebView托管了该网页。我可以在其中托管我的网页。但是 WebView 中的文本框无法接受输入,并且超链接无法响应。
这是我的代码。我创建了一个名为 WebPageOemViewer 的 WPF 用户控件。
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>
代码隐藏:
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();
}
}
}
网页将显示在窗口中。但是搜索框和超链接没有响应。我也尝试了最新的CefSharp.Wpf。两者都不能工作。
string address = "http://www.bing.com";
FillUserInfoWindow fills = new FillUserInfoWindow(address);
fills.ShowDialog();
您可以通过上面的代码重现此问题。谢谢你的时间。
最后,我从 https://github.com/cefsharp/CefSharp/issues/1080#issuecomment-111969127 那里得到了答案
很抱歉在网上发布重复的线程。
在阿梅特兰的帮助下。我解决了这个问题。我们只需要删除OnMouseLeftButtonDown方法。
谢谢你的时间。