refactor: 将Framework库和RT、UI放在同一个解决方案中。
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace OpenSEMI.ClientBase.Command
|
||||
{
|
||||
public class BaseCommand<T> : ICommand
|
||||
{
|
||||
#region Fields
|
||||
|
||||
readonly Action<T> _execute = null;
|
||||
readonly Predicate<T> _canExecute = null;
|
||||
|
||||
#endregion // Fields
|
||||
|
||||
#region Constructors
|
||||
|
||||
public BaseCommand(Action<T> execute)
|
||||
: this(execute, null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command.
|
||||
/// </summary>
|
||||
/// <param name="execute">The execution logic.</param>
|
||||
/// <param name="canExecute">The execution status logic.</param>
|
||||
public BaseCommand(Action<T> execute, Predicate<T> canExecute)
|
||||
{
|
||||
if (execute == null)
|
||||
throw new ArgumentNullException("execute");
|
||||
|
||||
_execute = execute;
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
#endregion // Constructors
|
||||
|
||||
#region ICommand Members
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
return _canExecute == null ? true : _canExecute((T)parameter);
|
||||
}
|
||||
|
||||
public event EventHandler CanExecuteChanged
|
||||
{
|
||||
add
|
||||
{
|
||||
if (_canExecute != null)
|
||||
CommandManager.RequerySuggested += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
if (_canExecute != null)
|
||||
CommandManager.RequerySuggested -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
_execute((T)parameter);
|
||||
}
|
||||
|
||||
#endregion // ICommand Members
|
||||
}
|
||||
|
||||
public class BaseCommand : ICommand
|
||||
{
|
||||
#region Fields
|
||||
|
||||
readonly Action _execute;
|
||||
readonly Func<bool> _canExecute;
|
||||
|
||||
#endregion // Fields
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command that can always execute.
|
||||
/// </summary>
|
||||
/// <param name="execute">The execution logic.</param>
|
||||
public BaseCommand(Action execute)
|
||||
: this(execute, null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command.
|
||||
/// </summary>
|
||||
/// <param name="execute">The execution logic.</param>
|
||||
/// <param name="canExecute">The execution status logic.</param>
|
||||
public BaseCommand(Action execute, Func<bool> canExecute)
|
||||
{
|
||||
if (execute == null)
|
||||
throw new ArgumentNullException("execute");
|
||||
|
||||
_execute = execute;
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
#endregion // Constructors
|
||||
|
||||
#region ICommand Members
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
return _canExecute == null ? true : _canExecute();
|
||||
}
|
||||
|
||||
public event EventHandler CanExecuteChanged
|
||||
{
|
||||
add
|
||||
{
|
||||
if (_canExecute != null)
|
||||
CommandManager.RequerySuggested += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
if (_canExecute != null)
|
||||
CommandManager.RequerySuggested -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
_execute();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
|
||||
namespace OpenSEMI.ClientBase.Command
|
||||
{
|
||||
public class CommandParameter<TCustomParameter>
|
||||
{
|
||||
public TCustomParameter CustomParameter { get; private set; }
|
||||
|
||||
protected CommandParameter(TCustomParameter customParameter)
|
||||
{
|
||||
this.CustomParameter = customParameter;
|
||||
}
|
||||
|
||||
public static CommandParameter<TCustomParameter> Cast(object parameter)
|
||||
{
|
||||
var parameterToCast = parameter as CommandParameter<object>;
|
||||
if (parameterToCast == null)
|
||||
{
|
||||
throw new InvalidCastException(string.Format("Failed to case {0} to {1}",
|
||||
parameter.GetType(), typeof(CommandParameter<object>)));
|
||||
}
|
||||
|
||||
var castedParameter = new CommandParameter<TCustomParameter>(
|
||||
(TCustomParameter)parameterToCast.CustomParameter);
|
||||
|
||||
return castedParameter;
|
||||
}
|
||||
}
|
||||
|
||||
public class EventCommandParameter<TCustomParameter, TEventArgs> : CommandParameter<TCustomParameter>
|
||||
where TEventArgs : RoutedEventArgs
|
||||
{
|
||||
public RoutedEvent RoutedEvent { get; private set; }
|
||||
public TEventArgs EventArgs { get; private set; }
|
||||
|
||||
public EventCommandParameter(
|
||||
TCustomParameter customParameter,
|
||||
RoutedEvent routedEvent,
|
||||
TEventArgs eventArgs)
|
||||
: base(customParameter)
|
||||
{
|
||||
this.RoutedEvent = routedEvent;
|
||||
this.EventArgs = eventArgs;
|
||||
}
|
||||
|
||||
public static new EventCommandParameter<TCustomParameter, TEventArgs> Cast(object parameter)
|
||||
{
|
||||
var parameterToCast = parameter as EventCommandParameter<object, RoutedEventArgs>;
|
||||
if (parameterToCast == null)
|
||||
{
|
||||
throw new InvalidCastException(string.Format("Failed to case {0} to {1}",
|
||||
parameter.GetType(), typeof(EventCommandParameter<object, RoutedEventArgs>)));
|
||||
}
|
||||
|
||||
var castedParameter = new EventCommandParameter<TCustomParameter, TEventArgs>(
|
||||
(TCustomParameter)parameterToCast.CustomParameter,
|
||||
parameterToCast.RoutedEvent,
|
||||
(TEventArgs)parameterToCast.EventArgs);
|
||||
|
||||
return castedParameter;
|
||||
}
|
||||
}
|
||||
|
||||
public class PropertyCommandParameter<TCustomParameter, TValue> : CommandParameter<TCustomParameter>
|
||||
{
|
||||
public DependencyProperty Property { get; private set; }
|
||||
public TValue Value { get; private set; }
|
||||
|
||||
public PropertyCommandParameter(
|
||||
TCustomParameter customParameter,
|
||||
DependencyProperty property,
|
||||
TValue value)
|
||||
: base(customParameter)
|
||||
{
|
||||
this.Property = property;
|
||||
this.Value = value;
|
||||
}
|
||||
|
||||
public static new PropertyCommandParameter<TCustomParameter, TValue> Cast(object parameter)
|
||||
{
|
||||
var parameterToCast = parameter as PropertyCommandParameter<object, object>;
|
||||
if (parameterToCast == null)
|
||||
{
|
||||
throw new InvalidCastException(string.Format("Failed to case {0} to {1}",
|
||||
parameter.GetType(), typeof(PropertyCommandParameter<object, object>)));
|
||||
}
|
||||
|
||||
var castedParameter = new PropertyCommandParameter<TCustomParameter, TValue>(
|
||||
(TCustomParameter)parameterToCast.CustomParameter,
|
||||
parameterToCast.Property,
|
||||
(TValue)parameterToCast.Value);
|
||||
|
||||
return castedParameter;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace OpenSEMI.ClientBase.Command
|
||||
{
|
||||
public interface ICommandTrigger
|
||||
{
|
||||
void Initialize(FrameworkElement source);
|
||||
}
|
||||
|
||||
public static class CommandSource
|
||||
{
|
||||
|
||||
public static ICommandTrigger GetTrigger(FrameworkElement source)
|
||||
{
|
||||
return (ICommandTrigger)source.GetValue(TriggerProperty);
|
||||
}
|
||||
|
||||
public static void SetTrigger(FrameworkElement source, ICommandTrigger value)
|
||||
{
|
||||
source.SetValue(TriggerProperty, value);
|
||||
}
|
||||
|
||||
// Using a DependencyProperty as the backing store for Trigger. This enables animation, styling, binding, etc...
|
||||
public static readonly DependencyProperty TriggerProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"Trigger1",
|
||||
typeof(ICommandTrigger),
|
||||
typeof(CommandSource),
|
||||
new UIPropertyMetadata(
|
||||
null,
|
||||
TriggerPropertyChanged));
|
||||
|
||||
private static void TriggerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
FrameworkElement element = d as FrameworkElement;
|
||||
|
||||
ICommandTrigger commandTrigger = e.NewValue as ICommandTrigger;
|
||||
if (commandTrigger != null)
|
||||
{
|
||||
commandTrigger.Initialize(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
|
||||
|
||||
namespace OpenSEMI.ClientBase.Command
|
||||
{
|
||||
public abstract class CommandTrigger : Freezable, ICommandTrigger
|
||||
{
|
||||
public bool IsInitialized { get; set; }
|
||||
|
||||
#region Dependency Properties
|
||||
|
||||
#region Command Property
|
||||
|
||||
/// <value>Identifies the Command dependency property</value>
|
||||
public static readonly DependencyProperty CommandProperty =
|
||||
DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandTrigger),
|
||||
new FrameworkPropertyMetadata(null));
|
||||
|
||||
/// <value>description for Command property</value>
|
||||
public ICommand Command
|
||||
{
|
||||
get { return (ICommand)GetValue(CommandProperty); }
|
||||
set { SetValue(CommandProperty, value); }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region CustomParameter Property
|
||||
|
||||
/// <value>Identifies the CustomParameterProperty dependency property</value>
|
||||
public static readonly DependencyProperty CustomParameterProperty =
|
||||
DependencyProperty.Register("CustomParameter", typeof(object), typeof(CommandTrigger),
|
||||
new FrameworkPropertyMetadata(null));
|
||||
|
||||
/// <value>description for CustomParameter property</value>
|
||||
public object CustomParameter
|
||||
{
|
||||
get { return (object)GetValue(CustomParameterProperty); }
|
||||
set { SetValue(CustomParameterProperty, value); }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region CommandTarget Property
|
||||
|
||||
/// <value>Identifies the CommandTarget dependency property</value>
|
||||
public static readonly DependencyProperty CommandTargetProperty =
|
||||
DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(CommandTrigger),
|
||||
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
|
||||
|
||||
/// <value>description for CommandTarget property</value>
|
||||
public IInputElement CommandTarget
|
||||
{
|
||||
get { return (IInputElement)GetValue(CommandTargetProperty); }
|
||||
set { SetValue(CommandTargetProperty, value); }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region Internals
|
||||
|
||||
void ICommandTrigger.Initialize(FrameworkElement source)
|
||||
{
|
||||
if (IsInitialized)
|
||||
return;
|
||||
|
||||
InitializeCore(source);
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
protected abstract void InitializeCore(FrameworkElement source);
|
||||
|
||||
protected void ExecuteCommand(CommandParameter<object> parameter)
|
||||
{
|
||||
if (Command == null)
|
||||
return;
|
||||
|
||||
RoutedCommand routedCommand = Command as RoutedCommand;
|
||||
if (routedCommand != null)
|
||||
{
|
||||
routedCommand.Execute(parameter, CommandTarget);
|
||||
}
|
||||
else
|
||||
{
|
||||
Command.Execute(parameter);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
|
||||
namespace OpenSEMI.ClientBase.Command
|
||||
{
|
||||
public sealed class CommandTriggerGroup : FreezableCollection<CommandTrigger>, ICommandTrigger
|
||||
{
|
||||
private readonly HashSet<ICommandTrigger> _initList = new HashSet<ICommandTrigger>();
|
||||
|
||||
void ICommandTrigger.Initialize(FrameworkElement source)
|
||||
{
|
||||
foreach (ICommandTrigger child in this)
|
||||
{
|
||||
if (!_initList.Contains(child))
|
||||
{
|
||||
InitializeCommandSource(source, child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeCommandSource(FrameworkElement source, ICommandTrigger child)
|
||||
{
|
||||
child.Initialize(source);
|
||||
_initList.Add(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace OpenSEMI.ClientBase.Command
|
||||
{
|
||||
public sealed class EventCommandTrigger : CommandTrigger
|
||||
{
|
||||
#region Dependency Properties
|
||||
|
||||
#region RoutedEvent Property
|
||||
|
||||
/// <value>Identifies the RoutedEvent dependency property</value>
|
||||
public static readonly DependencyProperty RoutedEventProperty =
|
||||
DependencyProperty.Register("RoutedEvent", typeof(RoutedEvent), typeof(EventCommandTrigger),
|
||||
new FrameworkPropertyMetadata(null));
|
||||
|
||||
/// <value>description for RoutedEvent property</value>
|
||||
public RoutedEvent RoutedEvent
|
||||
{
|
||||
get { return (RoutedEvent)GetValue(RoutedEventProperty); }
|
||||
set { SetValue(RoutedEventProperty, value); }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
protected override Freezable CreateInstanceCore()
|
||||
{
|
||||
return new EventCommandTrigger();
|
||||
}
|
||||
|
||||
|
||||
protected override void InitializeCore(FrameworkElement source)
|
||||
{
|
||||
source.AddHandler(RoutedEvent, (RoutedEventHandler)ExecuteCommand);
|
||||
}
|
||||
|
||||
private void ExecuteCommand(object sender, RoutedEventArgs args)
|
||||
{
|
||||
CommandParameter<object> parameter = new EventCommandParameter<object, RoutedEventArgs>(
|
||||
CustomParameter, RoutedEvent, args);
|
||||
|
||||
base.ExecuteCommand(parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace OpenSEMI.ClientBase.Command
|
||||
{
|
||||
public class PropertyCommandTrigger : CommandTrigger
|
||||
{
|
||||
#region Dependency Properties
|
||||
|
||||
#region Property Property
|
||||
|
||||
/// <value>Identifies the Property dependency property</value>
|
||||
public static readonly DependencyProperty PropertyProperty =
|
||||
DependencyProperty.Register("Property", typeof(DependencyProperty), typeof(PropertyCommandTrigger),
|
||||
new FrameworkPropertyMetadata(null));
|
||||
|
||||
/// <value>description for Property property</value>
|
||||
public DependencyProperty Property
|
||||
{
|
||||
get { return (DependencyProperty)GetValue(PropertyProperty); }
|
||||
set { SetValue(PropertyProperty, value); }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Value Property
|
||||
|
||||
/// <value>Identifies the Value dependency property</value>
|
||||
public static readonly DependencyProperty ValueProperty =
|
||||
DependencyProperty.Register("Value", typeof(string), typeof(PropertyCommandTrigger),
|
||||
new FrameworkPropertyMetadata(null));
|
||||
|
||||
/// <value>description for Value property</value>
|
||||
public string Value
|
||||
{
|
||||
get { return (string)GetValue(ValueProperty); }
|
||||
set { SetValue(ValueProperty, value); }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
protected override Freezable CreateInstanceCore()
|
||||
{
|
||||
return new PropertyCommandTrigger();
|
||||
}
|
||||
|
||||
#region T Property
|
||||
|
||||
/// <value>Identifies the T dependency property</value>
|
||||
public static readonly DependencyProperty TProperty =
|
||||
DependencyProperty.Register("T", typeof(object), typeof(PropertyCommandTrigger),
|
||||
new FrameworkPropertyMetadata(null, OnTChanged));
|
||||
|
||||
/// <value>description for T property</value>
|
||||
public object T
|
||||
{
|
||||
get { return (object)GetValue(TProperty); }
|
||||
set { SetValue(TProperty, value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invoked on T change.
|
||||
/// </summary>
|
||||
/// <param name="d">The object that was changed</param>
|
||||
/// <param name="e">Dependency property changed event arguments</param>
|
||||
static void OnTChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
protected override void InitializeCore(FrameworkElement source)
|
||||
{
|
||||
DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(Property, source.GetType());
|
||||
descriptor.AddValueChanged(source, (s, e) =>
|
||||
{
|
||||
CommandParameter<object> parameter = new PropertyCommandParameter<object, object>(
|
||||
CustomParameter, Property, source.GetValue(Property));
|
||||
|
||||
object value = Value;
|
||||
if (descriptor.Converter.CanConvertFrom(typeof(string)))
|
||||
{
|
||||
value = descriptor.Converter.ConvertFromString(Value);
|
||||
}
|
||||
|
||||
if (object.Equals(source.GetValue(Property), value))
|
||||
ExecuteCommand(parameter);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user