Skip to content

新增关于事件转命令的类已经类库文件 #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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
12 changes: 12 additions & 0 deletions uwp/src/Framework/Framework/Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
<None Remove="ViewModel\MasterDetail\**" />
</ItemGroup>

<ItemGroup>
<Reference Include="PresentationCore">
<HintPath>..\FrameWorkLib\PresentationCore.dll</HintPath>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从本来的设计上,在 Framework 是不包含 WPF 和 UWP 的实现的

</Reference>
<Reference Include="System.Windows.Interactivity">
<HintPath>..\FrameWorkLib\System.Windows.Interactivity.dll</HintPath>
</Reference>
<Reference Include="WindowsBase">
<HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\WindowsBase.dll</HintPath>
</Reference>
</ItemGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'uap10.0'">
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
<NugetTargetMoniker>UAP,Version=v10.0</NugetTargetMoniker>
Expand Down
16 changes: 16 additions & 0 deletions uwp/src/Framework/Framework/ViewModel/EventInfomation.cs
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>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的代码没有对齐

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该限制 TEventArgs 是参数也就是加上 where TEventArgs 是具体的什么

{
public object Sender { get; set; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

理论上作为参数都应该只能 get 不能修改


public TEventArgs EventArgs { get; set; }


public object CommandParameter { get; set; }
}
}
13 changes: 13 additions & 0 deletions uwp/src/Framework/Framework/ViewModel/MapEventToCommand.cs
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>
{

}
}
104 changes: 104 additions & 0 deletions uwp/src/Framework/Framework/ViewModel/MapEventToCommandBase.cs
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在 abstract 和 class 存在两个空格

{

protected override void Invoke(object parameter)
{
if (base.AssociatedObject!=null)
Copy link
Owner

Choose a reason for hiding this comment

The 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,
Copy link
Owner

Choose a reason for hiding this comment

The 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;
}

Copy link
Owner

Choose a reason for hiding this comment

The 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);




}
}
23 changes: 23 additions & 0 deletions uwp/src/Framework/Framework/ViewModel/ViewModelCommand.cs
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; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

理论上不应该开放修改

}

}
}