Last active
January 2, 2016 07:28
-
-
Save softlion/8269736 to your computer and use it in GitHub Desktop.
TextViewImeActionBinding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Author: softlion (softlion.com) | |
/// </summary> | |
/// <remarks> | |
/// Thanks to: Stuart Lodge | |
/// </remarks> | |
public class TextViewImeActionBinding : MvxAndroidTargetBinding | |
{ | |
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"); | |
else | |
view.EditorAction += ViewOnEditorAction; | |
} | |
private void ViewOnEditorAction(object sender, TextView.EditorActionEventArgs args) | |
{ | |
if (args.Event != null) | |
{ | |
if (args.Event.IsShiftPressed) | |
{ | |
// if shift key is down, then we want to insert the '\n' char in the TextView; | |
// if event action is keydown then wait for keyup before excecuting it | |
args.Handled = false; | |
return; | |
} | |
} | |
args.Handled = true; | |
//Required test, otherwise it fires twice (with hardware keyboard). With sw kb args.Event is null. | |
if (command == null || (args.Event != null && args.Event.Action != KeyEventActions.Down)) | |
return; | |
command.Execute(null); | |
} | |
//Never called with OneWay binding mode | |
//public override void SubscribeToEvents() | |
public override Type TargetType { get { return typeof(ICommand); } } | |
//Should be OneWayToSource, but SetValueImpl is never called in this case | |
public override MvxBindingMode DefaultMode { get { return MvxBindingMode.OneWay; } } | |
protected override void SetValueImpl(object target, object value) | |
{ | |
command = value as ICommand; | |
if (command == null) | |
Mvx.Error("viewmodel target is not an ICommand: {0}", value!=null ? value.GetType() : null); | |
} | |
protected override void Dispose(bool isDisposing) | |
{ | |
if (isDisposing) | |
{ | |
var view = TextView; | |
if (view != null) | |
{ | |
view.EditorAction -= ViewOnEditorAction; | |
} | |
} | |
base.Dispose(isDisposing); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment