-
Notifications
You must be signed in to change notification settings - Fork 21
新增关于事件转命令的类已经类库文件 #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace lindexi.MVVM.Framework.ViewModel | ||
{ | ||
public class EventInfomation<TEventArgs> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的代码没有对齐 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 应该限制 TEventArgs 是参数也就是加上 where TEventArgs 是具体的什么 |
||
{ | ||
public object Sender { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 理论上作为参数都应该只能 get 不能修改 |
||
|
||
public TEventArgs EventArgs { get; set; } | ||
|
||
|
||
public object CommandParameter { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Windows.Interactivity; | ||
using System.Windows.Input; | ||
|
||
namespace lindexi.MVVM.Framework.ViewModel | ||
{ | ||
public class MapEventToCommand: MapEventToCommandBase<EventArgs> | ||
{ | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Windows; | ||
using System.Windows.Input; | ||
using System.Windows.Interactivity; | ||
|
||
namespace lindexi.MVVM.Framework.ViewModel | ||
{ | ||
public abstract class MapEventToCommandBase<TEventArgs>:TriggerAction<DependencyObject> where TEventArgs:EventArgs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在 abstract 和 class 存在两个空格 |
||
{ | ||
|
||
protected override void Invoke(object parameter) | ||
{ | ||
if (base.AssociatedObject!=null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在表达式左右需要加上空格 |
||
{ | ||
ICommand command = GetCommand(); | ||
EventInfomation<TEventArgs> eventInfomation = new EventInfomation<TEventArgs>() | ||
{ | ||
Sender=base.AssociatedObject, | ||
EventArgs=parameter as TEventArgs, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里有要求 parameter 一定就是 TEventArgs 不让就应该给异常 |
||
CommandParameter=GetValue(CommandParameterProperty) | ||
}; | ||
if (command!=null&&command.CanExecute(eventInfomation)) | ||
{ | ||
command.Execute(eventInfomation); | ||
} | ||
} | ||
} | ||
|
||
|
||
private ICommand GetCommand() | ||
{ | ||
ICommand command = null; | ||
if (this.Command!=null) | ||
{ | ||
command = this.Command; | ||
} | ||
else if(base.AssociatedObject!=null) | ||
{ | ||
Type type = base.AssociatedObject.GetType(); | ||
PropertyInfo[] propertyInfoArray = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); | ||
for (int i = 0; i < propertyInfoArray.Length; i++) | ||
{ | ||
PropertyInfo propertyInfo = propertyInfoArray[i]; | ||
if (typeof(ICommand).IsAssignableFrom(propertyInfo.PropertyType)&&string.Equals(propertyInfo.Name,this.CommandName,StringComparison.Ordinal)) | ||
{ | ||
command = (ICommand)propertyInfo.GetValue(base.AssociatedObject, null); | ||
} | ||
} | ||
} | ||
return command; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 多余的空行 |
||
|
||
|
||
|
||
private string _commandName; | ||
|
||
public string CommandName | ||
{ | ||
get | ||
{ | ||
base.ReadPreamble(); | ||
return this._commandName; | ||
} | ||
set | ||
{ | ||
if (this._commandName != value) | ||
{ | ||
base.WritePreamble(); | ||
this._commandName = value; | ||
base.WritePostscript(); | ||
} | ||
this._commandName = value; | ||
} | ||
} | ||
|
||
|
||
public ICommand Command | ||
{ | ||
get { return (ICommand)GetValue(CommandProperty); } | ||
set { SetValue(CommandProperty, value); } | ||
} | ||
|
||
public static readonly DependencyProperty CommandProperty = | ||
DependencyProperty.Register("Command", typeof(ICommand), typeof(MapEventToCommandBase<TEventArgs>), null); | ||
|
||
|
||
public object CommandParameter | ||
{ | ||
get { return (object)GetValue(CommandParameterProperty); } | ||
set { SetValue(CommandParameterProperty, value); } | ||
} | ||
|
||
public static readonly DependencyProperty CommandParameterProperty = | ||
DependencyProperty.Register("CommandParameter", typeof(object), typeof(MapEventToCommandBase<TEventArgs>), null); | ||
|
||
|
||
|
||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace lindexi.MVVM.Framework.ViewModel | ||
{ | ||
public class ViewModelCommand | ||
{ | ||
public ViewModelCommand(Action<object> action) | ||
{ | ||
this.ViewModelAction = action; | ||
} | ||
|
||
private Action<object> _viewModelAction; | ||
|
||
public Action<object> ViewModelAction | ||
{ | ||
get { return _viewModelAction; } | ||
set { _viewModelAction = value; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 理论上不应该开放修改 |
||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
从本来的设计上,在 Framework 是不包含 WPF 和 UWP 的实现的