I am currently building a small app using MVVM pattern.
I also implemented custom ViewRenderer by following the guide on building hybridwebview.
I wanted to extend this example a bit and add a button which will trigger Control.EvaluateJavascript(Control is platform specific WebView) to execute the script that's been injected.
I was able to get this working in two different ways, but I wasn't satisfied with either of these solutions
Binding Hack
- Define bindable property in the hybridwebview
- Create a boolean value in the viewmodel and bind it to the bindable property that was just created in the hybridwebview
- Define command for the button, which just flips the value of the boolean in the view model
- In the ViewRenderer, subscribe to PropertyChanged, and call Control.EvaluateJavascript
Bind HybridWebView as Button Command's Command Parameter
- Define command on the button.
- Bind the command parameter to hybridwebview via reference (eg: CommandParameter="{x:Reference hybridWebView}")
- Define EventHandler and a method which raises this event in hybridwebview
- In the view model, command for clicking the button will call the method which was defined in the view.
- Subscribe to the event in the ViewRenderer and call Control.EvaluateJavascript
How would I go about doing this while conforming to Xamarin best practices?
Thanks!