问题
I'm trying to create ChromiumOSR programmatically but I keep getting an error (access violation). Here is sample code that causes the problem:
var
pChromiumOSR: TChromiumOSR;
begin
pChromiumOSR := TChromiumOSR.Create(Self);
pChromiumOSR.OnLoadEnd := pChromiumOSRLoadEnd;
pChromiumOSR.Browser.MainFrame.LoadUrl('www.google.com');
end;
The problem is that pChromiumOSR.Browser.MainFrame is always nil. If I do pChromiumOSR.load('www.google.com'); I don't get any errors but it doesn't fire the onLoadend.
Can anyone give me any suggestions on what I might be doing wrong? I'm using Delphi XE2 but not sure which version of chromium (where can I find the version?)
回答1:
Your attempt to use Load
method for loading a page was correct. The other one was wrong and failed because the Browser
instance was not created. It's because the TChromiumOSR
was designed to be a design time component rather than to be created dynamically.
Now, the only place where the Browser
instance is created is the Loaded
method, which is called for a component after its parent form is loaded from a stream. And since you are creating it dynamically, the Browser
instance is never created.
For some reason also the CreateBrowser
method (which creates the Browser
instance) is declared as private, which complicates its calling a bit (unless you decide to modify the source and make it public). If you don't want to change your DCEF source code, you can use a class helper to provide access to the CreateBrowser
method:
uses
ceflib, cefvcl;
type
TChromiumOSRHelper = class helper for TCustomChromiumOSR
public
procedure CreateBrowserInstance;
end;
implementation
{ TChromiumOSRHelper }
procedure TChromiumOSRHelper.CreateBrowserInstance;
begin
Self.CreateBrowser;
end;
Then to create a Browser
instance add the CreateBrowserInstance
call before the first accessing the Browser
instance (which is here the Load
method):
var
pChromiumOSR: TChromiumOSR;
begin
pChromiumOSR := TChromiumOSR.Create(Self);
pChromiumOSR.OnLoadEnd := pChromiumOSRLoadEnd;
pChromiumOSR.CreateBrowserInstance;
pChromiumOSR.Load('www.google.com');
end;
来源:https://stackoverflow.com/questions/20251111/getting-errors-creating-chromiumosr-programmatically