Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1829 from tritter/feature/ios-control-events
Add support for all UIControlEvents
- Loading branch information
Showing
9 changed files
with
314 additions
and
214 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
209 changes: 209 additions & 0 deletions
209
MvvmCross/Binding/iOS/Target/MvxUIControlTargetBinding.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
// MvxUIControlTargetBinding.cs | ||
|
||
// MvvmCross is licensed using Microsoft Public License (Ms-PL) | ||
// Contributions and inspirations noted in readme.md and license.txt | ||
// | ||
// Project Lead - Stuart Lodge, @slodge, me@slodge.com | ||
|
||
namespace MvvmCross.Binding.iOS.Target | ||
{ | ||
using System; | ||
using System.Windows.Input; | ||
|
||
using MvvmCross.Binding.Bindings.Target; | ||
using MvvmCross.Platform.Platform; | ||
using MvvmCross.Platform.WeakSubscription; | ||
using UIKit; | ||
|
||
public class MvxUIControlTargetBinding : MvxConvertingTargetBinding | ||
{ | ||
private ICommand _command; | ||
private IDisposable _canExecuteSubscription; | ||
private readonly EventHandler<EventArgs> _canExecuteEventHandler; | ||
private readonly string _controlEvent; | ||
protected UIControl Control => base.Target as UIControl; | ||
|
||
public MvxUIControlTargetBinding(UIControl control, string controlEvent) | ||
: base(control) | ||
{ | ||
_controlEvent = controlEvent; | ||
|
||
if (control == null) | ||
{ | ||
MvxBindingTrace.Trace(MvxTraceLevel.Error, "Error - UIControl is null in MvxUIControlTargetBinding"); | ||
} | ||
else | ||
{ | ||
AddHandler(control); | ||
} | ||
|
||
this._canExecuteEventHandler = new EventHandler<EventArgs>(this.OnCanExecuteChanged); | ||
} | ||
|
||
private void ControlEvent(object sender, EventArgs eventArgs) | ||
{ | ||
if (this._command == null) | ||
return; | ||
|
||
if (!this._command.CanExecute(null)) | ||
return; | ||
|
||
this._command.Execute(null); | ||
} | ||
|
||
public override MvxBindingMode DefaultMode => MvxBindingMode.OneWay; | ||
|
||
public override System.Type TargetType => typeof(ICommand); | ||
|
||
protected override void SetValueImpl(object target, object value) | ||
{ | ||
if (this._canExecuteSubscription != null) | ||
{ | ||
this._canExecuteSubscription.Dispose(); | ||
this._canExecuteSubscription = null; | ||
} | ||
this._command = value as ICommand; | ||
if (this._command != null) | ||
{ | ||
this._canExecuteSubscription = this._command.WeakSubscribe(this._canExecuteEventHandler); | ||
} | ||
this.RefreshEnabledState(); | ||
} | ||
|
||
private void RefreshEnabledState() | ||
{ | ||
var view = this.Control; | ||
if (view == null) | ||
return; | ||
|
||
var shouldBeEnabled = false; | ||
if (this._command != null) | ||
{ | ||
shouldBeEnabled = this._command.CanExecute(null); | ||
} | ||
view.Enabled = shouldBeEnabled; | ||
} | ||
|
||
private void OnCanExecuteChanged(object sender, EventArgs e) | ||
{ | ||
this.RefreshEnabledState(); | ||
} | ||
|
||
protected override void Dispose(bool isDisposing) | ||
{ | ||
if (isDisposing) | ||
{ | ||
var view = this.Control; | ||
if (view != null) | ||
{ | ||
RemoveHandler(view); | ||
} | ||
if (this._canExecuteSubscription != null) | ||
{ | ||
this._canExecuteSubscription.Dispose(); | ||
this._canExecuteSubscription = null; | ||
} | ||
} | ||
base.Dispose(isDisposing); | ||
} | ||
|
||
private void AddHandler(UIControl control) | ||
{ | ||
switch (_controlEvent) | ||
{ | ||
case (MvxIosPropertyBinding.UIControl_TouchDown): | ||
control.TouchDown += this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_TouchDownRepeat): | ||
control.TouchDownRepeat += this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_TouchDragInside): | ||
control.TouchDragInside += this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_TouchUpInside): | ||
control.TouchUpInside += this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_ValueChanged): | ||
control.ValueChanged += this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_PrimaryActionTriggered): | ||
control.PrimaryActionTriggered += this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_EditingDidBegin): | ||
control.EditingDidBegin += this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_EditingChanged): | ||
control.EditingChanged += this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_EditingDidEnd): | ||
control.EditingDidEnd += this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_EditingDidEndOnExit): | ||
control.EditingDidEndOnExit += this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_AllTouchEvents): | ||
control.AllTouchEvents += this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_AllEditingEvents): | ||
control.AllEditingEvents += this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_AllEvents): | ||
control.AllEvents += this.ControlEvent; | ||
break; | ||
default: | ||
MvxBindingTrace.Trace(MvxTraceLevel.Error, "Error - Invalid controlEvent in MvxUIControlTargetBinding"); | ||
break; | ||
} | ||
|
||
} | ||
|
||
private void RemoveHandler(UIControl control) | ||
{ | ||
switch (_controlEvent) | ||
{ | ||
case (MvxIosPropertyBinding.UIControl_TouchDown): | ||
control.TouchDown -= this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_TouchDownRepeat): | ||
control.TouchDownRepeat -= this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_TouchDragInside): | ||
control.TouchDragInside -= this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_TouchUpInside): | ||
control.TouchUpInside -= this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_ValueChanged): | ||
control.ValueChanged -= this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_PrimaryActionTriggered): | ||
control.PrimaryActionTriggered -= this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_EditingDidBegin): | ||
control.EditingDidBegin -= this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_EditingChanged): | ||
control.EditingChanged -= this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_EditingDidEnd): | ||
control.EditingDidEnd -= this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_EditingDidEndOnExit): | ||
control.EditingDidEndOnExit -= this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_AllTouchEvents): | ||
control.AllTouchEvents -= this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_AllEditingEvents): | ||
control.AllEditingEvents -= this.ControlEvent; | ||
break; | ||
case (MvxIosPropertyBinding.UIControl_AllEvents): | ||
control.AllEvents -= this.ControlEvent; | ||
break; | ||
default: | ||
MvxBindingTrace.Trace(MvxTraceLevel.Error, "Error - Invalid controlEvent in MvxUIControlTargetBinding"); | ||
break; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.