I am using CefSharp for WPF. I want to use it in MVVM architecture. Unfortunately there is a problem. In the view I have:
<ContentPresenter Content="{Binding Browser}"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
In ViewModel I put a new object into Browser
:
var settings = new CefSharp.Settings
{
PackLoadingDisabled = true,
}
if (CefSharp.CEF.Initialize(settings))
{
int counter = 0;
this.Browser = new WebView();
}
Unfortunately I cannot Load any URL at any point after that. It says Browser not initialized
and actually the IsBrowserInitialized
property (in Browser
) is false
.
That is weird because in test app, not MVVM, where I used same code to instantiate the WebView it works. Only difference is that I programmatically added the Browser to a Grid as it was not MVVM.
Anyone got the CefSharp in WPF MVVM app? Any ideas?
Thanks
EDIT:
I have noticed in test non-MVVM app, that the IsBrowserInitialized
property is set to false until window constructor ends the job.
CefSharp version 1 is not very well suited for MVVM at the moment, unfortunately. Luckily, in CefSharp 3 I have tried to make it more "MVVM-aware" so you don't have to do a lot of hacks. See the CefSharp.Wpf.Example code there and you will (hopefully) see what I mean.
The way to get it working with CefSharp version 1.xx is to set up a PropertyChanged
event handler. Once the IsBrowserInitialized
property gets set to true, you can then do your work. Like this:
webView.PropertyChanged += OnWebViewPropertyChanged;
// ...
private void OnWebViewPropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "IsBrowserInitialized":
if (webView.IsBrowserInitialized)
{
webView.Load("http://some/url");
}
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With