Skip to content

Instantly share code, notes, and snippets.

@slodge
Forked from softlion/TextViewImeActionBinding.axml
Last active January 2, 2016 07:39
Show Gist options
  • Save slodge/8270923 to your computer and use it in GitHub Desktop.
Save slodge/8270923 to your computer and use it in GitHub Desktop.
public class TextViewImeActionBinding : MvxAndroidTargetBinding
{
private bool subscribed;
private ICommand _command;
protected TextView TextView
{
get { return Target as TextView; }
}
public static void Register(IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterCustomBindingFactory<TextView>("ImeAction", view => new TextViewImeActionBinding(view));
}
public TextViewImeActionBinding(TextView view) : base(view)
{
if (view == null)
Mvx.Trace(MvxTraceLevel.Error, "TextViewImeActionBinding : view is null");
if (view != null)
{
TextView.EditorAction += ViewOnEditorAction;
subscribed = true;
}
}
private void ViewOnEditorAction(object sender, TextView.EditorActionEventArgs args)
{
FireValueChanged(args.ActionId);
//args.ActionId
args.Handled = true;
if (_command == null)
return;
_command.Execute(((TextView)sender).Text);
}
public override Type TargetType
{
get { return typeof(ICommand); }
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.OneWay; }
}
protected override void SetValueImpl(object target, object value)
{
_command = (ICommand)value;
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
var view = TextView;
if (view != null && subscribed)
{
view.EditorAction -= ViewOnEditorAction;
}
}
base.Dispose(isDisposing);
}
}
<TextView
local:MvxBind="Text PasswordConfirm; ImeAction RegisterCommand"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:imeOptions="actionSend" />
@softlion
Copy link

softlion commented Jan 5, 2014

Oh ! It works !!

I checked value in the debugger, but was not able to find an ICommand in it. Should be caused by an "incorrect" binding mode.

But should'nt it be a OneWayToSource binding ? (from target view to source viewmodel ?)

And because the binding mode is set to OneWay, SubscribeToEvents is not called anymore. That's why you removed it :)

I update the gist, as i fixed other errors. You can put the code in vnext if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment