Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The framework can automatically bind any events which require EventHandler types. However, for any events which require a templated EventHandler (with custom EventArgs) then you are correct - you'll need to include a custom Binding.</p> <p>The good news is that custom bindings are easy to write and to include.</p> <p>For example, to bind:</p> <pre class="lang-csharp prettyprint-override"><code>public event EventHandler&lt;View.LongClickEventArgs&gt; LongClick; </code></pre> <p>you can include something like:</p> <pre class="lang-csharp prettyprint-override"><code>public class LongPressEventBinding : MvxBaseAndroidTargetBinding { private readonly View _view; private IMvxCommand _command; public LongPressEventBinding(View view) { _view = view; _view.LongClick += ViewOnLongClick; } private void ViewOnLongClick(object sender, View.LongClickEventArgs eventArgs) { if (_command != null) { _command.Execute(); } } public override void SetValue(object value) { _command = (IMvxCommand)value; } protected override void Dispose(bool isDisposing) { if (isDisposing) { _view.Click -= ViewOnLongClick; } base.Dispose(isDisposing); } public override Type TargetType { get { return typeof(IMvxCommand); } } public override MvxBindingMode DefaultMode { get { return MvxBindingMode.OneWay; } } } </code></pre> <p>Which can be configured in setup using something like:</p> <pre class="lang-csharp prettyprint-override"><code> protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry) { base.FillTargetFactories(registry); registry.RegisterFactory(new MvxCustomBindingFactory&lt;View&gt;("LongPress", view =&gt; new LongPressEventBinding(view))); } </code></pre> <hr> <p>Note that you can't write a single class that binds to all the different event types - as the compiler requires you to include the correct Type for the EventArgs. However, you could fairly easily change <code>public class LongClickEventBinding</code> to something like <code>public class CustomEventBinding&lt;TViewType, TEventArgsType&gt;</code> if you wanted to.</p> <hr> <p>With regards to what argument you should pass into the IMvxCommand Execute method, I guess this depends a bit on the method in question, and it also depends on whether you need the ViewModel to support multiple platforms, or whether it is just for Android.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload