Could someone explain to me how the CefSharp LoadHtml
function works?
LoadHtml(string html, string url)
What do the html
and url
parameters represent?
I am interested in loading a page from a raw HTML string into the CefSharp browser.
Could someone explain to me how the CefSharp LoadHtml
function works?
LoadHtml(string html, string url)
What do the html
and url
parameters represent?
I am interested in loading a page from a raw HTML string into the CefSharp browser.
In
LoadHtml(string html, string url)
:html
is your HTML string, e.g."<html><body>Hello world</body></html>"
. Actually, you can even put other content in the string, such as SVG markup, as long as Chromium can understand it.url
is needed because your HTML code may contain JavaScript that tries to perform AJAX calls, and the web browser needs to understand what security restrictions apply. The scheme (e.g. "http:", "about:") and domain (e.g. "localhost", "google.com") affect behaviour such as clicking on links, AJAX requests, iframes, etc.If you want to simply render static HTML, make the
url
something unique such ashttp://rendering/
(so that the resource handler does not overlap with a realurl
on the web). If you need to load the HTML and then interact with it or perform AJAX calls, choose aurl
that matches the domain you want to interact with - for example, if you want to make an alternative Google home page and perform AJAX search queries, you will want to usehttps://www.google.com/
as your URL so you can communicate with it.You can see the source code for
LoadHtml
here.What CefSharp does is:
url
.Load(url)
to tell Chromium to load the givenurl
.Then, under the hood:
url
.html
.html
instead of the real content of the URL.Try the following code
Hope this helps.
For a WPF project, try the following.
Create a namespace reference to CefSharp.Wpf in the xaml.
Add the ChromiumWebBrowser element to your window.
Remember to assign a name to the element (in this case the element is called browser). We will use it to call the LoadHtml method later on.
Create an event handler for the IsBrowserInitializedChanged event. This is important, because this event will be fired once the ChromiumWebBrowser control is ready. Then can we load html.
Putting it all together...
MainWindow.xaml
MainWindow.xaml.cs