Reputation: 2591
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.
Upvotes: 4
Views: 6409
Reputation: 1273
I appreciate it's an old question and possibly answered elsewhere, but given the succinct title google may send you here (it did for me!)
The underlying ChromiumWebBrowser is very MVVM friendly..
Example..
<cefSharp:ChromiumWebBrowser name="browser" WebBrowser="{Binding WebBrowser, Mode=OneWayToSource}"
Address="{Binding Address, Mode=TwoWay}"
RenderOptions.BitmapScalingMode="{Binding ElementName=scalingModeComboBox, Path=SelectedItem}" />
Upvotes: -1
Reputation: 4220
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;
}
}
Upvotes: 8