Hello,
I'm fairly new (student working part-time) to programming in developing applications with WPF, so I'd appreciate any help.
Just some background information as to what we (the company I work in) would like to accomplish, without delving into too much detail:
Our website lists several thousand job offers and various bits of information about those job offers from our database. But instead of piecing together the container with all the information on our page separately and every single time, our idea was to access and (pre-)render the HTML string of every page and then store it in a table of its own. The HTML string is then accessed asynchronously (JavaScript/AJAX).
Up until now, we have used the framework WatIn to accomplish that feat, but we would to like to implement CefSharp instead.
I wrote a sample WPF application and implemented Cef, which loads the specified page, gets the source code (GetSourceAsync()) and then saves it to a simple .txt file. The problem that arises in my case is that only the "skeleton" (if I may call it that) of our webpage is loaded, but without the needed container.
This is essentially my (sample) ChromeWrapper class:
` public class ChromeWrapper : IDisposable
{
private readonly ChromiumWebBrowser _browser;
private string _html = "";
private bool _pageIsLoaded = false;
public ChromeWrapper()
{
var settings = new CefSettings();
settings.CefCommandLineArgs.Add("disable-gpu", "1"); // Disable GPU in WPF and Offscreen examples until #1634 has been resolved
Cef.Initialize(settings, shutdownOnProcessExit: true, performDependencyCheck: true);
_browser = new ChromiumWebBrowser();//new ChromiumWebBrowser(string.Empty);
_browser.BrowserSettings.ApplicationCache = CefState.Disabled;
}
public string GetHtml(string url)
{
_pageIsLoaded = false;
while (!_browser.IsBrowserInitialized)
Thread.Sleep(100);
_browser.Load(url);
_browser.LoadingStateChanged += BrowserLoadingStateChanged;
while (!_pageIsLoaded)
Thread.Sleep(100);
return _html;
}
private void BrowserLoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
// Check to see if loading is complete - this event is called twice, one when loading starts
// second time when it's finished
// (rather than an iframe within the main frame).
if (e.IsLoading)
return;
_browser.LoadingStateChanged -= BrowserLoadingStateChanged;
var htmlTask = e.Browser.MainFrame.GetSourceAsync();
htmlTask.ContinueWith(obj =>
{
_html = htmlTask.Result;
_pageIsLoaded = true;
});
}
public void Dispose()
{
if (_browser != null)
_browser.Dispose();
Cef.Shutdown();
}
}`
My MainWindow class simply instantiates ChromeWrapper and loads some random page from our website.
What version of the product are you using?
Nuget
What architecture x86 or x64?
x86
On what operating system?
Win7
Are you using WinForms, WPF or OffScreen?
WPF