This blog is dedicated to my experiences in programming. Contributors are welcome, please email me at jhelyn.suan@gmail.com
Friday, November 27, 2009
Overview of Implementation of Peer-to-Peer in C#
Peer-to-Peer(P2P) architecture is a serverless architecture. In a peer mesh network, in order for them to be considered and classified as peer, first, the node needs to be included in a peer mesh layer. To do so, their are two options. First by using PNRP(Peer Name Resolution Protocol) and Custom Peer Resolver.
Managed Extensibility Framework(MEF)
MEF is the creation of extensible applications. It offers discovery and composition capabilities that you can leverage to load application extensions (.dll's).
It provides a standard way for the host application to expose itself and consume external extensions; sets of discovery approaches for your application to locate and load available extensions; and tagging extensions with additonal metadata which facilitates querying and filtering.
It provides a standard way for the host application to expose itself and consume external extensions; sets of discovery approaches for your application to locate and load available extensions; and tagging extensions with additonal metadata which facilitates querying and filtering.
Monday, November 23, 2009
Eventing in MVVM pattern without using Caliburn Framework
Steps:
1. You need to have an EventBehaviourFactory class. You can copy this code.
public static class EventBehaviourFactory
{
public static DependencyProperty CreateCommandExecutionEventBehaviour(RoutedEvent routedEvent, string propertyName, Type ownerType)
{
DependencyProperty property = DependencyProperty.RegisterAttached(propertyName, typeof(ICommand), ownerType,
new PropertyMetadata(null,
new ExecuteCommandOnRoutedEventBehaviour(routedEvent).PropertyChangedHandler));
return property;
}
///
/// An internal class to handle listening for an event and executing a command,
/// when a Command is assigned to a particular DependencyProperty
///
private class ExecuteCommandOnRoutedEventBehaviour : ExecuteCommandBehaviour
{
private readonly RoutedEvent _routedEvent;
public ExecuteCommandOnRoutedEventBehaviour(RoutedEvent routedEvent)
{
_routedEvent = routedEvent;
}
///
/// Handles attaching or Detaching Event handlers when a Command is assigned or unassigned
///
///
///
///
protected override void AdjustEventHandlers(DependencyObject sender, object oldValue, object newValue)
{
UIElement element = sender as UIElement;
if (element == null) { return; }
if (oldValue != null)
{
element.RemoveHandler(_routedEvent, new RoutedEventHandler(EventHandler));
}
if (newValue != null)
{
element.AddHandler(_routedEvent, new RoutedEventHandler(EventHandler));
}
}
protected void EventHandler(object sender, RoutedEventArgs e)
{
HandleEvent(sender, e);
}
}
internal abstract class ExecuteCommandBehaviour
{
protected DependencyProperty _property;
protected abstract void AdjustEventHandlers(DependencyObject sender, object oldValue, object newValue);
protected void HandleEvent(object sender, EventArgs e)
{
DependencyObject dp = sender as DependencyObject;
if (dp == null)
{
return;
}
ICommand command = dp.GetValue(_property) as ICommand;
if (command == null)
{
return;
}
if (command.CanExecute(e))
{
command.Execute(e);
}
}
///
/// Listens for a change in the DependencyProperty that we are assigned to, and
/// adjusts the EventHandlers accordingly
///
///
///
public void PropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
// the first time the property changes,
// make a note of which property we are supposed
// to be watching
if (_property == null)
{
_property = e.Property;
}
object oldValue = e.OldValue;
object newValue = e.NewValue;
AdjustEventHandlers(sender, oldValue, newValue);
}
}
}
2. Create a relay command class which inherits ICommand Interface. You can copy this code. An ICommand contains methods to execute commands. A command can be executed many times, and the parameter values can vary. This interface is mandatory on commands.
Namespace: System.Windows.Input
Assembly: PresentationCore (in PresentationCore.dll)
public class RelayCommand:ICommand
{
#region private fields
readonly Action
1. You need to have an EventBehaviourFactory class. You can copy this code.
public static class EventBehaviourFactory
{
public static DependencyProperty CreateCommandExecutionEventBehaviour(RoutedEvent routedEvent, string propertyName, Type ownerType)
{
DependencyProperty property = DependencyProperty.RegisterAttached(propertyName, typeof(ICommand), ownerType,
new PropertyMetadata(null,
new ExecuteCommandOnRoutedEventBehaviour(routedEvent).PropertyChangedHandler));
return property;
}
///
/// An internal class to handle listening for an event and executing a command,
/// when a Command is assigned to a particular DependencyProperty
///
private class ExecuteCommandOnRoutedEventBehaviour : ExecuteCommandBehaviour
{
private readonly RoutedEvent _routedEvent;
public ExecuteCommandOnRoutedEventBehaviour(RoutedEvent routedEvent)
{
_routedEvent = routedEvent;
}
///
/// Handles attaching or Detaching Event handlers when a Command is assigned or unassigned
///
///
///
///
protected override void AdjustEventHandlers(DependencyObject sender, object oldValue, object newValue)
{
UIElement element = sender as UIElement;
if (element == null) { return; }
if (oldValue != null)
{
element.RemoveHandler(_routedEvent, new RoutedEventHandler(EventHandler));
}
if (newValue != null)
{
element.AddHandler(_routedEvent, new RoutedEventHandler(EventHandler));
}
}
protected void EventHandler(object sender, RoutedEventArgs e)
{
HandleEvent(sender, e);
}
}
internal abstract class ExecuteCommandBehaviour
{
protected DependencyProperty _property;
protected abstract void AdjustEventHandlers(DependencyObject sender, object oldValue, object newValue);
protected void HandleEvent(object sender, EventArgs e)
{
DependencyObject dp = sender as DependencyObject;
if (dp == null)
{
return;
}
ICommand command = dp.GetValue(_property) as ICommand;
if (command == null)
{
return;
}
if (command.CanExecute(e))
{
command.Execute(e);
}
}
///
/// Listens for a change in the DependencyProperty that we are assigned to, and
/// adjusts the EventHandlers accordingly
///
///
///
public void PropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
// the first time the property changes,
// make a note of which property we are supposed
// to be watching
if (_property == null)
{
_property = e.Property;
}
object oldValue = e.OldValue;
object newValue = e.NewValue;
AdjustEventHandlers(sender, oldValue, newValue);
}
}
}
2. Create a relay command class which inherits ICommand Interface. You can copy this code. An ICommand contains methods to execute commands. A command can be executed many times, and the parameter values can vary. This interface is mandatory on commands.
Namespace: System.Windows.Input
Assembly: PresentationCore (in PresentationCore.dll)
public class RelayCommand:ICommand
{
#region private fields
readonly Action
Labels:
C#,
computer programming,
Events,
ICommand,
MVVM,
programming,
WPF,
XAML
Subscribe to:
Posts (Atom)