wpf CefSharp 3 和 SetZoomLevel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37388909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CefSharp 3 and SetZoomLevel
提问by Marcel Grüger
I am creating many ChromiumWebBrowser-Instances programmatically inside my app. On some time later I set the visibility and an address for the browser. Also I wanted to change the zoom-level. But whenever I try to change it in the normal way (like with a simple
我在我的应用程序中以编程方式创建了许多 ChromiumWebBrowser-Instances。一段时间后,我为浏览器设置了可见性和地址。我也想改变缩放级别。但是每当我尝试以正常方式更改它时(例如使用简单的
browser.ZoomLevel = (Convert.ToDouble(browser.Tag) - 100) / 25.0;
I only get an error :IBrowser instance is null. Browser has likely not finished initializing or is in the process of disposing.But when I can set the Address, why cant I set the ZoomLevel?
Even if I try to put a method in the FrameLoadEndand change the ZoomLevel there, I only get an error, that its on another thread. Shouldn't the event been fired on the same thread? I cannot access the sender of the event inside the event? strange...
Has someone an idea how I can change the ZoomLevel programmatically? I have to change it on load of the site and later by button.
Oh, and btw. I am using the wpf-version of CefSharp 3.
我只收到一个错误:IBrowser instance is null. Browser has likely not finished initializing or is in the process of disposing.但是当我可以设置地址时,为什么不能设置 ZoomLevel?
即使我尝试在 中放置一个方法FrameLoadEnd并在那里更改 ZoomLevel,我也只会收到一个错误,它在另一个线程上。事件不应该在同一个线程上触发吗?我无法访问事件内部的事件发送者?奇怪......
有人知道我如何以编程方式更改 ZoomLevel 吗?我必须在网站加载时更改它,然后通过按钮进行更改。
哦,顺便说一句。我正在使用 CefSharp 3 的 wpf 版本。
采纳答案by Marcel Grüger
Ok, for everyone who wants to know the working answer here it is:
好的,对于每个想知道工作答案的人来说,它是:
On creation I added an eventhandler
在创建时,我添加了一个事件处理程序
myBrowser.FrameLoadEnd += MyBrowserOnFrameLoadEnd;
That looks like this
看起来像这样
private void MyBrowserOnFrameLoadEnd(object sender, FrameLoadEndEventArgs frameLoadEndEventArgs)
{
ChromiumWebBrowser browser = (ChromiumWebBrowser) sender;
Dispatcher.Invoke(() =>
{
ZoomLevelTextBox.Text = ((Convert.ToDouble(browser.Tag) - 100) / 25.0).ToString(CultureInfo.CurrentCulture);
browser.SetZoomLevel((Convert.ToDouble(browser.Tag) - 100) / 25.0);
});
}
And later you can change that with two buttons
然后你可以用两个按钮改变它
private void IncreaseZoomOnPreview_OnClick(object sender, RoutedEventArgs e)
{
if (_selectedPreview < 0 || _previewItems[_selectedPreview] == null)
return;
ChangeZoom(0.5); //You could also use 0.1 or 1.0, as you like and in the decrease button you use -0.5, etc.
}
And the final answer to the dispatching/tasking and so on
以及对调度/任务等的最终答复
private void ChangeZoom(double change)
{
PreviewItem previewItem = _previewItems[_selectedPreview];
ChromiumWebBrowser browser = new ChromiumWebBrowser();
foreach (object child in ((Canvas)previewItem.PreviewBorder.Child).Children)
{
browser = child as ChromiumWebBrowser;
if (browser != null)
break;
}
Task<double> task = browser.GetZoomLevelAsync();
task.ContinueWith(previous =>
{
if (previous.IsCompleted)
{
double currentLevel = previous.Result;
browser.SetZoomLevel(currentLevel + change);
}
else
{
throw new InvalidOperationException("Unexpected failure of calling CEF->GetZoomLevelAsync", previous.Exception);
}
}, TaskContinuationOptions.ExecuteSynchronously);
ZoomLevelTextBox.Text = (Convert.ToDouble(ZoomLevelTextBox.Text) + change).ToString(CultureInfo.CurrentCulture);
}
The maximum is a value of -10 to 10. So you should ask for that on a click also or set the values in a ListBox or ComboBox, etc.
最大值是 -10 到 10 的值。因此,您也应该在单击时询问或在 ListBox 或 ComboBox 等中设置值。
回答by Peter Gruppelaar
It was not working for me but i have a fix for other ppl with the same problem. I will make a new topic for this fix, it will save time. this took me 5 hours to figure out. IsLoaded check is the fix...
它对我不起作用,但我为其他具有相同问题的人提供了解决方案。我将为此修复程序创建一个新主题,它将节省时间。这花了我 5 个小时才弄明白。IsLoaded 检查是修复...
public async void ExecuteJavaScript(string script)
{
if (browser != null)
if (browser.CanExecuteJavascriptInMainFrame)
{
System.Windows.Forms.MessageBox.Show(browser.IsLoaded.ToString());
if(browser.IsLoaded == true)
browser.ExecuteScriptAsync(script);
}
await Task.Delay(5000);
}

