0

I've some issues:

I've a web browser from cefSharp on which I'm trying to trigger a command on load:

My command is currently empty:

        LoadedCommand = new RelayCommand(() =>
        {

        });

The issue is that when the event is triggered, before even entering in my command, I get an InvalidOperationException about a cross thread exception.

For what I see here it seems that this event is triggered from a thread that is not the UI thread.

I've the feeling that the EventToCommand class tries to get some UI stuff which cause the application to crash.

How would you address this issue?

J4N
  • 19,480
  • 39
  • 187
  • 340

1 Answers1

1

You could do this by using a Behaviour.

public class ChromiumLoadedBehaviour : System.Windows.Interactivity.Behavior<CefSharp.Wpf.ChromiumWebBrowser>
{
    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(ChromiumLoadedBehaviour), new PropertyMetadata(null));


    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.Loaded += AssociatedObject_Loaded;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        this.AssociatedObject.Loaded -= AssociatedObject_Loaded;
    }

    private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
    {
        Command?.Execute(null);
    }
}

If you dont know - how to use the Behaviour - check out this answer here

Additional information

Maybe use the FrameLoadEnd event.

Peter
  • 1,655
  • 22
  • 44