[英]How can I use a ChromiumWebBrowser when Collapsed
I have a ChromiumWebBrowser
control with Visibility
of Collapsed
.我有一个
Visibility
of Collapsed
的ChromiumWebBrowser
控件。 Until the visibility is set to Visible
, the page is not loaded and many calls fail with:在可见性设置为
Visible
,页面不会加载并且许多调用失败:
System.Exception
: The browser has not been initialized.System.Exception
: 浏览器尚未初始化。Load
can only be called after the underlying CEF browser is initialized (CefLifeSpanHandler::OnAfterCreated
).Load
只能在底层 CEF 浏览器初始化后调用(CefLifeSpanHandler::OnAfterCreated
)。
How can I have the page be loaded when the Visibility
is Collapsed
?当
Visibility
Collapsed
时如何加载页面?
Create a subclass of ChromiumWebBrowser
as described in Render problems when Initial Visibility Collapsed .创建
ChromiumWebBrowser
的子类,如初始可见性折叠时呈现问题中所述。 To work around a NullReferenceException
in AbstractRenderHandler.OnPaint
, you will need to ensure the visual tree is already created (by calling ApplyTemplate
).要解决
AbstractRenderHandler.OnPaint
的NullReferenceException
,您需要确保已创建可视化树(通过调用ApplyTemplate
)。
Example:例子:
internal sealed class CollapsableChromiumWebBrowser : ChromiumWebBrowser
{
public CollapsableChromiumWebBrowser()
{
this.Loaded += this.CollapsableChromiumWebBrowser_Loaded;
}
private void CollapsableChromiumWebBrowser_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
// Avoid loading CEF in designer
if (DesignerProperties.GetIsInDesignMode(this))
{
return;
}
// Avoid NRE in AbstractRenderHandler.OnPaint
ApplyTemplate();
// https://github.com/cefsharp/CefSharp/issues/1412
CreateOffscreenBrowser(new Size(400, 400));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.