Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 817221c

Browse files
committed
Code layout fixed.
1 parent b39c520 commit 817221c

File tree

4 files changed

+202
-184
lines changed

4 files changed

+202
-184
lines changed

Behaviors/EventToCommandBehavior/EventToCommandBehavior/Behaviors/BehaviorBase.cs

100755100644
Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,40 @@
33

44
namespace EventToCommandBehavior
55
{
6-
public class BehaviorBase<T> : Behavior<T> where T : BindableObject
7-
{
8-
public T AssociatedObject { get; private set; }
9-
10-
protected override void OnAttachedTo (T bindable)
11-
{
12-
base.OnAttachedTo (bindable);
13-
AssociatedObject = bindable;
14-
15-
if (bindable.BindingContext != null) {
16-
BindingContext = bindable.BindingContext;
17-
}
18-
19-
bindable.BindingContextChanged += OnBindingContextChanged;
20-
}
21-
22-
protected override void OnDetachingFrom (T bindable)
23-
{
24-
base.OnDetachingFrom (bindable);
25-
bindable.BindingContextChanged -= OnBindingContextChanged;
26-
AssociatedObject = null;
27-
}
28-
29-
void OnBindingContextChanged (object sender, EventArgs e)
30-
{
31-
OnBindingContextChanged ();
32-
}
33-
34-
protected override void OnBindingContextChanged ()
35-
{
36-
base.OnBindingContextChanged ();
37-
BindingContext = AssociatedObject.BindingContext;
38-
}
39-
}
6+
public class BehaviorBase<T> : Behavior<T> where T : BindableObject
7+
{
8+
public T AssociatedObject { get; private set; }
9+
10+
protected override void OnAttachedTo(T bindable)
11+
{
12+
base.OnAttachedTo(bindable);
13+
AssociatedObject = bindable;
14+
15+
if (bindable.BindingContext != null)
16+
{
17+
BindingContext = bindable.BindingContext;
18+
}
19+
20+
bindable.BindingContextChanged += OnBindingContextChanged;
21+
}
22+
23+
protected override void OnDetachingFrom(T bindable)
24+
{
25+
base.OnDetachingFrom(bindable);
26+
bindable.BindingContextChanged -= OnBindingContextChanged;
27+
AssociatedObject = null;
28+
}
29+
30+
void OnBindingContextChanged(object sender, EventArgs e)
31+
{
32+
OnBindingContextChanged();
33+
}
34+
35+
protected override void OnBindingContextChanged()
36+
{
37+
base.OnBindingContextChanged();
38+
BindingContext = AssociatedObject.BindingContext;
39+
}
40+
}
4041
}
4142

Behaviors/EventToCommandBehavior/EventToCommandBehavior/Behaviors/EventToCommandBehavior.cs

100755100644
Lines changed: 124 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -5,111 +5,128 @@
55

66
namespace EventToCommandBehavior
77
{
8-
public class EventToCommandBehavior : BehaviorBase<VisualElement>
9-
{
10-
Delegate eventHandler;
11-
12-
public static readonly BindableProperty EventNameProperty = BindableProperty.Create ("EventName", typeof(string), typeof(EventToCommandBehavior), null, propertyChanged: OnEventNameChanged);
13-
public static readonly BindableProperty CommandProperty = BindableProperty.Create ("Command", typeof(ICommand), typeof(EventToCommandBehavior), null);
14-
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create ("CommandParameter", typeof(object), typeof(EventToCommandBehavior), null);
15-
public static readonly BindableProperty InputConverterProperty = BindableProperty.Create ("Converter", typeof(IValueConverter), typeof(EventToCommandBehavior), null);
16-
17-
public string EventName {
18-
get { return (string)GetValue (EventNameProperty); }
19-
set { SetValue (EventNameProperty, value); }
20-
}
21-
22-
public ICommand Command {
23-
get { return (ICommand)GetValue (CommandProperty); }
24-
set { SetValue (CommandProperty, value); }
25-
}
26-
27-
public object CommandParameter {
28-
get { return GetValue (CommandParameterProperty); }
29-
set { SetValue (CommandParameterProperty, value); }
30-
}
31-
32-
public IValueConverter Converter {
33-
get { return (IValueConverter)GetValue (InputConverterProperty); }
34-
set { SetValue (InputConverterProperty, value); }
35-
}
36-
37-
protected override void OnAttachedTo (VisualElement bindable)
38-
{
39-
base.OnAttachedTo (bindable);
40-
RegisterEvent (EventName);
41-
}
42-
43-
protected override void OnDetachingFrom (VisualElement bindable)
44-
{
45-
DeregisterEvent (EventName);
46-
base.OnDetachingFrom (bindable);
47-
}
48-
49-
void RegisterEvent (string name)
50-
{
51-
if (string.IsNullOrWhiteSpace (name)) {
52-
return;
53-
}
54-
55-
EventInfo eventInfo = AssociatedObject.GetType ().GetRuntimeEvent (name);
56-
if (eventInfo == null) {
57-
throw new ArgumentException (string.Format ("EventToCommandBehavior: Can't register the '{0}' event.", EventName));
58-
}
59-
MethodInfo methodInfo = typeof(EventToCommandBehavior).GetTypeInfo ().GetDeclaredMethod ("OnEvent");
60-
eventHandler = methodInfo.CreateDelegate (eventInfo.EventHandlerType, this);
61-
eventInfo.AddEventHandler (AssociatedObject, eventHandler);
62-
}
63-
64-
void DeregisterEvent (string name)
65-
{
66-
if (string.IsNullOrWhiteSpace (name)) {
67-
return;
68-
}
69-
70-
if (eventHandler == null) {
71-
return;
72-
}
73-
EventInfo eventInfo = AssociatedObject.GetType ().GetRuntimeEvent (name);
74-
if (eventInfo == null) {
75-
throw new ArgumentException (string.Format ("EventToCommandBehavior: Can't de-register the '{0}' event.", EventName));
76-
}
77-
eventInfo.RemoveEventHandler (AssociatedObject, eventHandler);
78-
eventHandler = null;
79-
}
80-
81-
void OnEvent (object sender, object eventArgs)
82-
{
83-
if (Command == null) {
84-
return;
85-
}
86-
87-
object resolvedParameter;
88-
if (CommandParameter != null) {
89-
resolvedParameter = CommandParameter;
90-
} else if (Converter != null) {
91-
resolvedParameter = Converter.Convert (eventArgs, typeof(object), null, null);
92-
} else {
93-
resolvedParameter = eventArgs;
94-
}
95-
96-
if (Command.CanExecute (resolvedParameter)) {
97-
Command.Execute (resolvedParameter);
98-
}
99-
}
100-
101-
static void OnEventNameChanged (BindableObject bindable, object oldValue, object newValue)
102-
{
103-
var behavior = (EventToCommandBehavior)bindable;
104-
if (behavior.AssociatedObject == null) {
105-
return;
106-
}
107-
108-
string oldEventName = (string)oldValue;
109-
string newEventName = (string)newValue;
110-
111-
behavior.DeregisterEvent (oldEventName);
112-
behavior.RegisterEvent (newEventName);
113-
}
114-
}
8+
public class EventToCommandBehavior : BehaviorBase<VisualElement>
9+
{
10+
Delegate eventHandler;
11+
12+
public static readonly BindableProperty EventNameProperty = BindableProperty.Create("EventName", typeof(string), typeof(EventToCommandBehavior), null, propertyChanged: OnEventNameChanged);
13+
public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(EventToCommandBehavior), null);
14+
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create("CommandParameter", typeof(object), typeof(EventToCommandBehavior), null);
15+
public static readonly BindableProperty InputConverterProperty = BindableProperty.Create("Converter", typeof(IValueConverter), typeof(EventToCommandBehavior), null);
16+
17+
public string EventName
18+
{
19+
get { return (string)GetValue(EventNameProperty); }
20+
set { SetValue(EventNameProperty, value); }
21+
}
22+
23+
public ICommand Command
24+
{
25+
get { return (ICommand)GetValue(CommandProperty); }
26+
set { SetValue(CommandProperty, value); }
27+
}
28+
29+
public object CommandParameter
30+
{
31+
get { return GetValue(CommandParameterProperty); }
32+
set { SetValue(CommandParameterProperty, value); }
33+
}
34+
35+
public IValueConverter Converter
36+
{
37+
get { return (IValueConverter)GetValue(InputConverterProperty); }
38+
set { SetValue(InputConverterProperty, value); }
39+
}
40+
41+
protected override void OnAttachedTo(VisualElement bindable)
42+
{
43+
base.OnAttachedTo(bindable);
44+
RegisterEvent(EventName);
45+
}
46+
47+
protected override void OnDetachingFrom(VisualElement bindable)
48+
{
49+
DeregisterEvent(EventName);
50+
base.OnDetachingFrom(bindable);
51+
}
52+
53+
void RegisterEvent(string name)
54+
{
55+
if (string.IsNullOrWhiteSpace(name))
56+
{
57+
return;
58+
}
59+
60+
EventInfo eventInfo = AssociatedObject.GetType().GetRuntimeEvent(name);
61+
if (eventInfo == null)
62+
{
63+
throw new ArgumentException(string.Format("EventToCommandBehavior: Can't register the '{0}' event.", EventName));
64+
}
65+
MethodInfo methodInfo = typeof(EventToCommandBehavior).GetTypeInfo().GetDeclaredMethod("OnEvent");
66+
eventHandler = methodInfo.CreateDelegate(eventInfo.EventHandlerType, this);
67+
eventInfo.AddEventHandler(AssociatedObject, eventHandler);
68+
}
69+
70+
void DeregisterEvent(string name)
71+
{
72+
if (string.IsNullOrWhiteSpace(name))
73+
{
74+
return;
75+
}
76+
77+
if (eventHandler == null)
78+
{
79+
return;
80+
}
81+
EventInfo eventInfo = AssociatedObject.GetType().GetRuntimeEvent(name);
82+
if (eventInfo == null)
83+
{
84+
throw new ArgumentException(string.Format("EventToCommandBehavior: Can't de-register the '{0}' event.", EventName));
85+
}
86+
eventInfo.RemoveEventHandler(AssociatedObject, eventHandler);
87+
eventHandler = null;
88+
}
89+
90+
void OnEvent(object sender, object eventArgs)
91+
{
92+
if (Command == null)
93+
{
94+
return;
95+
}
96+
97+
object resolvedParameter;
98+
if (CommandParameter != null)
99+
{
100+
resolvedParameter = CommandParameter;
101+
}
102+
else if (Converter != null)
103+
{
104+
resolvedParameter = Converter.Convert(eventArgs, typeof(object), null, null);
105+
}
106+
else
107+
{
108+
resolvedParameter = eventArgs;
109+
}
110+
111+
if (Command.CanExecute(resolvedParameter))
112+
{
113+
Command.Execute(resolvedParameter);
114+
}
115+
}
116+
117+
static void OnEventNameChanged(BindableObject bindable, object oldValue, object newValue)
118+
{
119+
var behavior = (EventToCommandBehavior)bindable;
120+
if (behavior.AssociatedObject == null)
121+
{
122+
return;
123+
}
124+
125+
string oldEventName = (string)oldValue;
126+
string newEventName = (string)newValue;
127+
128+
behavior.DeregisterEvent(oldEventName);
129+
behavior.RegisterEvent(newEventName);
130+
}
131+
}
115132
}

Behaviors/EventToCommandBehavior/EventToCommandBehavior/Converters/SelectedItemEventArgsToSelectedItemConverter.cs

100755100644
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
namespace EventToCommandBehavior
66
{
7-
public class SelectedItemEventArgsToSelectedItemConverter : IValueConverter
8-
{
9-
public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
10-
{
11-
var eventArgs = value as SelectedItemChangedEventArgs;
12-
return eventArgs.SelectedItem;
13-
}
7+
public class SelectedItemEventArgsToSelectedItemConverter : IValueConverter
8+
{
9+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
10+
{
11+
var eventArgs = value as SelectedItemChangedEventArgs;
12+
return eventArgs.SelectedItem;
13+
}
1414

15-
public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
16-
{
17-
throw new NotImplementedException ();
18-
}
19-
}
15+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
16+
{
17+
throw new NotImplementedException();
18+
}
19+
}
2020
}
2121

0 commit comments

Comments
 (0)