Zapraszam do kolejnego artykułu, tym razem o wtyczce NuGet:
http://msdn.microsoft.com/pl-pl/library/nuget–system-dystrybucji-bibliotek
Zapraszam do kolejnego artykułu, tym razem o wtyczce NuGet:
http://msdn.microsoft.com/pl-pl/library/nuget–system-dystrybucji-bibliotek
Artykuł dla osób, które dopiero zaczynają z testami jednostkowymi. Wprowadzenie, trochę teorii i best practises:
http://msdn.microsoft.com/pl-pl/library/testy-jednostkowe-w-visual-studio
Większość dzisiejszych aplikacji typu desktop posiada bufor stanu – popularnie nazywany “undo\redo” (skróty CTRL+Z, CTRL+Y). Najpopularniejszym zastosowaniem są edytory tekstu. Pisząc aplikację w C# z wykorzystaniem standardowym kontrolek TextBox taką funkcjonalność już będziemy mieli. Jednak czasami zachodzi rozwinięcie standardowej funkcjonalności o elementy specyficzne dla danej aplikacji. Najczęściej występuje to w różnego rodzaju edytorach (np. edytory map). W dzisiejszym poście przyjrzymy się implementacji w c#.
Kluczem do rozwiązania problemu jest zapamiętywanie w sprytny sposób stanu aplikacji lub danych potrzebnych do odtworzenia tego stanu. Zdefiniujmy więc interfejs opisujący stan:
public interface IUndoRedoState { void Commit(); void Rollback(); }
Każdy stan można zatwierdzić lub odwołać (cofnąć). Załóżmy, że piszemy kalkulator obsługujący między innymi operację dodawania. Wtedy taki stan może zostać zdefiniowany następująco:
public class SumUndoRedoState: IUndoRedoState { private int _Number=-1; private Calculator _Calc = null; public SumUndoRedostate(Calculator calculator,int operand ) { _Number = operand; _Calc = calculator; } public void Commit() { _Calc.Result += _Number; } public void Rollback() { _Calc.Result -= _Number; } }
Potrzebna nam jeszcze jedna klasa – manager który będzie zarządzał stanami:
class UndoRedoManager { public IList<UndoRedoState> m_States = new List<UndoRedoState>(); private int m_CurrentStateIndex = 0; public void AddState(UndoRedoState state) { state.Commit(); m_States.Insert(m_CurrentStateIndex, state); m_States.RemoveRange(m_CurrentStateIndex + 1, m_States.Count - 1 - m_CurrentStateIndex); m_CurrentStateIndex++; if (m_States.Count > 5) { m_States.RemoveAt(0); m_CurrentStateIndex = m_States.Count; } } public void Clear() { m_CurrentStateIndex = 0; m_States.Clear(); } public bool CanBeCommited { get { if (m_CurrentStateIndex < m_States.Count) return true; else return false; } } public bool CanBeRollbacked { get { if (m_CurrentStateIndex - 1 >= 0) return true; else return false; } } public void CommitLast() { if (CanBeCommited == false) return; UndoRedoState state = m_States[m_CurrentStateIndex]; state.Commit(); m_CurrentStateIndex++; } public void RollbackLast() { if (CanBeRollbacked == false) return; UndoRedoState state = m_States[m_CurrentStateIndex - 1]; state.Rollback(); m_CurrentStateIndex--; } }
Manager zawiera kolekcję wszystkich stanów. Pierwsza metoda (AddState) dodaje stan do kolekcji i wykonuje Commit. Użytkownik zatem może wywołać:
UndoRedoManager manager=new UndoRedoManager(); manager.AddState( new SumUndoRedoState(calc,10) );
Manager kolejno doda stan do kolekcji, wykona operację (dodanie liczby 10), usunie stany występujące w kolekcji po aktualnym stanie aplikacji a na końcu zaktualizuje indeks aktualnego stanu – m_CurrentStateIndex.
Manager zawiera również metodę do wyczyszczenia stanów (Clear) oraz właściwości sprawdzające czy operacja Commit\Rollback może być wykonana – można powiązać pod przyciski Undo\Redo w interfejsie.
Metody CommitLast, RollbackLast powinny być wywoływane w momencie Redo oraz Undo (Ctrl-Y, Ctrl-Z).
Oczywiście operacja dodania jest typowo akademickim przykładem. W praktyce jednak podobną konstrukcję można wykorzystać dla złożonych zadań. Osobiście skorzystałem z tego rozwiązania w swoim edytorze map, który wykonuje różne operacje typu “Dodaj obiekt”, “Modyfikuj siatkę terenu z zadanym współczynnikiem wgniecenia” itp.
Wzorzec MVVM jest najpopularniejszym rozwiązaniem architektonicznym dla WPF. Ze względu na wbudowany mechanizm wiązań, programiści chętnie sięgają po ten wzorzec projektowy. Jednym z problemów jest podpięcie zdarzenia. W większości przypadków możemy powiązać komendę za pomocą właściwości Command. Co jednak w przypadku gdy potrzebujemy specyficzne zdarzenie np. OnMouseDown? WPF niestety nie udostępnia właściwości typu OnClickCommand – do dyspozycji mamy tylko OnClick zwracający EventHandler a nie ICommand.
Rozwiązaniem są zachowania – obiekty doczepiane do kontrolek które mogą zmieniać ich zachowanie. Behaviour to klasa mająca referencję do kontrolki. Można więc stworzyć behaviour który podczepia się pod zdarzenie a następnie wywołuje naszą komendę. Istnieje już gotowa biblioteka implementująca stosowny behaviour – Marlon Grech prezentował ją na swoim blogu. Cały kod wraz z przykładem można ściągnąć właśnie z jego blogu a konkretnie z stąd. Dla osób którzy nie chcą ściągać całego dema wklejam kod wymaganych klas (źródło Marlon Grech):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Markup; using System.Windows; using System.Windows.Input; namespace AttachedCommandBehavior { /// <summary> /// Defines the attached properties to create a CommandBehaviorBinding /// </summary> public class CommandBehavior { #region Behavior /// <summary> /// Behavior Attached Dependency Property /// </summary> private static readonly DependencyProperty BehaviorProperty = DependencyProperty.RegisterAttached("Behavior", typeof(CommandBehaviorBinding), typeof(CommandBehavior), new FrameworkPropertyMetadata((CommandBehaviorBinding)null)); /// <summary> /// Gets the Behavior property. /// </summary> private static CommandBehaviorBinding GetBehavior(DependencyObject d) { return (CommandBehaviorBinding)d.GetValue(BehaviorProperty); } /// <summary> /// Sets the Behavior property. /// </summary> private static void SetBehavior(DependencyObject d, CommandBehaviorBinding value) { d.SetValue(BehaviorProperty, value); } #endregion #region Command /// <summary> /// Command Attached Dependency Property /// </summary> public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(CommandBehavior), new FrameworkPropertyMetadata((ICommand)null, new PropertyChangedCallback(OnCommandChanged))); /// <summary> /// Gets the Command property. /// </summary> public static ICommand GetCommand(DependencyObject d) { return (ICommand)d.GetValue(CommandProperty); } /// <summary> /// Sets the Command property. /// </summary> public static void SetCommand(DependencyObject d, ICommand value) { d.SetValue(CommandProperty, value); } /// <summary> /// Handles changes to the Command property. /// </summary> private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(d); binding.Command = (ICommand)e.NewValue; } #endregion #region CommandParameter /// <summary> /// CommandParameter Attached Dependency Property /// </summary> public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(CommandBehavior), new FrameworkPropertyMetadata((object)null, new PropertyChangedCallback(OnCommandParameterChanged))); /// <summary> /// Gets the CommandParameter property. /// </summary> public static object GetCommandParameter(DependencyObject d) { return (object)d.GetValue(CommandParameterProperty); } /// <summary> /// Sets the CommandParameter property. /// </summary> public static void SetCommandParameter(DependencyObject d, object value) { d.SetValue(CommandParameterProperty, value); } /// <summary> /// Handles changes to the CommandParameter property. /// </summary> private static void OnCommandParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(d); binding.CommandParameter = e.NewValue; } #endregion #region Event /// <summary> /// Event Attached Dependency Property /// </summary> public static readonly DependencyProperty EventProperty = DependencyProperty.RegisterAttached("Event", typeof(string), typeof(CommandBehavior), new FrameworkPropertyMetadata((string)String.Empty, new PropertyChangedCallback(OnEventChanged))); /// <summary> /// Gets the Event property. This dependency property /// indicates .... /// </summary> public static string GetEvent(DependencyObject d) { return (string)d.GetValue(EventProperty); } /// <summary> /// Sets the Event property. This dependency property /// indicates .... /// </summary> public static void SetEvent(DependencyObject d, string value) { d.SetValue(EventProperty, value); } /// <summary> /// Handles changes to the Event property. /// </summary> private static void OnEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(d); //check if the Event is set. If yes we need to rebind the Command to the new event and unregister the old one if (binding.Event != null && binding.Owner != null) binding.Dispose(); //bind the new event to the command binding.BindEvent(d, e.NewValue.ToString()); } #endregion #region Helpers //tries to get a CommandBehaviorBinding from the element. Creates a new instance if there is not one attached private static CommandBehaviorBinding FetchOrCreateBinding(DependencyObject d) { CommandBehaviorBinding binding = CommandBehavior.GetBehavior(d); if (binding == null) { binding = new CommandBehaviorBinding(); CommandBehavior.SetBehavior(d, binding); } return binding; } #endregion } }
CommandBehaviourBinding:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; using System.Reflection; using System.Windows; namespace AttachedCommandBehavior { /// <summary> /// Defines the command behavior binding /// </summary> public class CommandBehaviorBinding : IDisposable { #region Properties /// <summary> /// Get the owner of the CommandBinding ex: a Button /// This property can only be set from the BindEvent Method /// </summary> public DependencyObject Owner { get; private set; } /// <summary> /// The command to execute when the specified event is raised /// </summary> public ICommand Command { get; set; } /// <summary> /// Gets or sets a CommandParameter /// </summary> public object CommandParameter { get; set; } /// <summary> /// The event name to hook up to /// This property can only be set from the BindEvent Method /// </summary> public string EventName { get; private set; } /// <summary> /// The event info of the event /// </summary> public EventInfo Event { get; private set; } /// <summary> /// Gets the EventHandler for the binding with the event /// </summary> public Delegate EventHandler { get; private set; } #endregion //Creates an EventHandler on runtime and registers that handler to the Event specified public void BindEvent(DependencyObject owner, string eventName) { EventName = eventName; Owner = owner; Event = Owner.GetType().GetEvent(EventName, BindingFlags.Public | BindingFlags.Instance); if (Event == null) throw new InvalidOperationException(String.Format("Could not resolve event name {0}", EventName)); //Create an event handler for the event that will call the ExecuteCommand method EventHandler = EventHandlerGenerator.CreateDelegate( Event.EventHandlerType, typeof(CommandBehaviorBinding).GetMethod("ExecuteCommand", BindingFlags.Public | BindingFlags.Instance), this); //Register the handler to the Event Event.AddEventHandler(Owner, EventHandler); } /// <summary> /// Executes the command /// </summary> public void ExecuteCommand() { if (Command.CanExecute(CommandParameter)) Command.Execute(CommandParameter); } #region IDisposable Members bool disposed = false; /// <summary> /// Unregisters the EventHandler from the Event /// </summary> public void Dispose() { if (!disposed) { Event.RemoveEventHandler(Owner, EventHandler); disposed = true; } } #endregion } }
EventHandlerGenerator:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection.Emit; using System.Reflection; namespace AttachedCommandBehavior { /// <summary> /// Generates delegates according to the specified signature on runtime /// </summary> public static class EventHandlerGenerator { /// <summary> /// Generates a delegate with a matching signature of the supplied eventHandlerType /// This method only supports Events that have a delegate of type void /// </summary> /// <param name="eventInfo">The delegate type to wrap. Note that this must always be a void delegate</param> /// <param name="methodToInvoke">The method to invoke</param> /// <param name="methodInvoker">The object where the method resides</param> /// <returns>Returns a delegate with the same signature as eventHandlerType that calls the methodToInvoke inside</returns> public static Delegate CreateDelegate(Type eventHandlerType, MethodInfo methodToInvoke, object methodInvoker) { //Get the eventHandlerType signature var eventHandlerInfo = eventHandlerType.GetMethod("Invoke"); Type returnType = eventHandlerInfo.ReturnParameter.ParameterType; if (returnType != typeof(void)) throw new ApplicationException("Delegate has a return type. This only supprts event handlers that are void"); ParameterInfo[] delegateParameters = eventHandlerInfo.GetParameters(); //Get the list of type of parameters. Please note that we do + 1 because we have to push the object where the method resides i.e methodInvoker parameter Type[] hookupParameters = new Type[delegateParameters.Length + 1]; hookupParameters[0] = methodInvoker.GetType(); for (int i = 0; i < delegateParameters.Length; i++) hookupParameters[i + 1] = delegateParameters[i].ParameterType; DynamicMethod handler = new DynamicMethod("", null, hookupParameters, typeof(EventHandlerGenerator)); ILGenerator eventIL = handler.GetILGenerator(); //load the parameters or everything will just BAM :) LocalBuilder local = eventIL.DeclareLocal(typeof(object[])); eventIL.Emit(OpCodes.Ldc_I4, delegateParameters.Length + 1); eventIL.Emit(OpCodes.Newarr, typeof(object)); eventIL.Emit(OpCodes.Stloc, local); //start from 1 because the first item is the instance. Load up all the arguments for (int i = 1; i < delegateParameters.Length + 1; i++) { eventIL.Emit(OpCodes.Ldloc, local); eventIL.Emit(OpCodes.Ldc_I4, i); eventIL.Emit(OpCodes.Ldarg, i); eventIL.Emit(OpCodes.Stelem_Ref); } eventIL.Emit(OpCodes.Ldloc, local); //Load as first argument the instance of the object for the methodToInvoke i.e methodInvoker eventIL.Emit(OpCodes.Ldarg_0); //Now that we have it all set up call the actual method that we want to call for the binding eventIL.EmitCall(OpCodes.Call, methodToInvoke, null); eventIL.Emit(OpCodes.Pop); eventIL.Emit(OpCodes.Ret); //create a delegate from the dynamic method return handler.CreateDelegate(eventHandlerType, methodInvoker); } } }
Kodu jak widać jest sporo. Wykorzystanie jednak jest już łatwe:
<Border Background="Aqua" local:CommandBehavior.Event="MouseDown" local:CommandBehavior.Command="{Binding SomeCommand}" local:CommandBehavior.CommandParameter="{Binding}"/>
W powyższym przykładzie bindujemy komendę SomeCommand do zdarzenia MouseDown. Jako parametr zdarzenia otrzymamy stosowne EventArgs.
Dziś kolej na MVC 3.0. W pierwszej części podstawy:
http://msdn.microsoft.com/pl-pl/library/wprowadzenie-do-asp-net-mvc-3-0
Dla tych którzy chcą poznać sam silnik Razor zapraszam tutaj:
http://msdn.microsoft.com/pl-pl/library/razor–nowy-silnik-renderujacy
Krótki tekst o instalacji MVC 3.0 w chmurze Azure:
http://msdn.microsoft.com/pl-pl/library/asp-net-mvc-3-0-w-azure