Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

What is the Best Practice for Raising Custom Events in the MVVM Pattern?

$
0
0

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

    1. Define bindable property in the hybridwebview
    2. Create a boolean value in the viewmodel and bind it to the bindable property that was just created in the hybridwebview
    3. Define command for the button, which just flips the value of the boolean in the view model
    4. In the ViewRenderer, subscribe to PropertyChanged, and call Control.EvaluateJavascript
  • Bind HybridWebView as Button Command's Command Parameter

    1. Define command on the button.
    2. Bind the command parameter to hybridwebview via reference (eg: CommandParameter="{x:Reference hybridWebView}")
    3. Define EventHandler and a method which raises this event in hybridwebview
    4. In the view model, command for clicking the button will call the method which was defined in the view.
    5. 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!


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>