C# pass parameters to event handler - events

A question that has come to my mind. Let's say I want to pass a variable theVar to the event handler declaration below
new EventHandler<AsyncCompletedEventArgs>(evHandler)
and receive it in the definition below:
public void evHandler(object sender, EventArgs e)
{
}
How do I proceed?

If you are the person writing the code that's raising the event, Mr. Hopkinson puts it very nicely in his answer. You need a custom EventArgs with properties to represent the data you hope to pass.
But if you are merely consuming the event rather than raising it, your options are more limited.
Since you're anticipating receiving an AsyncCompletedEventArgs from your event, though, you do have one option: the UserState propery of AsyncCompletedEventArgs. This is a property you get to supply when you call the asynchronous method that ultimately causes the event to be fired. It can be any object you choose. You supply it when you call the async method, and the event returns it to you in the event argument properties when the method call completes.

Define a descendant of EventArgs
e.g.
public class MySpecialEventArgs :EventArgs
{
public int theVar {get; private set;}
public MySpecialEventArgs(int argVar)
{
theVar = argVar;
}
}
Then when you raise the event throw one of the above in instead of a EventArgs
When you add your handler e will be a MySpecialEventArgs.

Related

Mvc3 custom event hooks

I have a Mvc3-Project where I want to register to custom event hooks.
So that I can register to an event like "User logon". I do not want to do it in the controller, because there is a business logic behind it in an other project.
So in my Mvc3-Project I want to write some classes that will have the code that has to be executed when a User is loged on. But how do I register these classes (or an instance of them) to the event. Is it a good idea to use reflection an search for all classes inherited from a special base class, or is there an other smarter way?
So again, I do not want to monitor the action that is called, I want that the business logic triggers some classes in my Mvc3-Project.
EDIT
As Chris points out in the comments below, MVC3 is stateless, meaning that with this solution you would have to resubscribe for these events on every request. This is probably not a very good solution for MVC.
Have you considered an global event service?
Rough example:
class Example : IEventReceiver
{
public void Init()
{
EventService.Subscribe("Logon", this);
}
private void OnEvent(string eventName)
{
// Do logon stuff here.
}
}
You would need to create the EventService class, which might be a singleton or service. It might have interface similar to the following:
public interface IEventService
{
void Subscribe(string eventName, IEventReceiver receiver);
void Unsubscribe(string eventName, IEventReceiver receiver);
void DispatchEvent(string eventName);
}
public interface IEventReceiver
{
void OnEvent(string eventName);
}

Static classes and "this" keyword

I have a static class with a custom event in it, as below:
//The delegate
public static delegate void eventDoneDelegate(object sender, WebLoaderEventArgs e);
//The event that uses the delegate
public static event eventDoneDelegate PageRequestDone;
//Calls the event
private static void onPageRequestDoneChanged(WebLoaderEventArgs e)
{
if (PageRequestDone != null)
PageRequestDone(this, e);
}
I know "this" can't be used because it references the current instance, but how can I pass the currect class as a parameter?
Or maybe my logic is wrong, please aware me as I am new to this.
Thanks.
The semantic meaning of the sender argument value depends on a vague agreement between the event publisher and the event subscribers; there is no universal standard. In your example, I can't see any need for a sender value at all, you might as well pass null.

Handling the OnNavigatedFrom / OnNavigatedTo events in the ViewModel

I am trying to figure out a way for my ViewModel to handle saving or restore the page's state when the page is navigated From or To.
The first thing I tried was to add an EventToCommand behavior to the page, but the events (OnNavigatedFrom and OnNavigatedTo) are declared protected and the EventToCommand does not see the events to bind to.
Next I thought I would try using the Messenger class to pass a message to the ViewModel using code in the View's code behind:
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
Messenger.Default.Send<PhoneApplicationPage>(this);
base.OnNavigatedFrom(e);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
Messenger.Default.Send<PhoneApplicationPage>(this);
base.OnNavigatedTo(e);
}
But this seems to have two issues, first is having this code in the code behind page. Second, the ViewModel cannot tell the difference between the OnNavigatedFrom and the OnNavigatedTo events without having to create a set a wrapper classes for the PhoneApplicationPage object (see UPDATE below).
What is the most MVVM-Light friendly way to handle these events?
UPDATE:
I was able to resolve the second issue by Sending the Messages like this:
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
Messenger.Default.Send<PhoneApplicationPage>(this,"NavigatedFrom");
base.OnNavigatedFrom(e);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
Messenger.Default.Send<PhoneApplicationPage>(this, "NavigatedTo");
base.OnNavigatedTo(e);
}
and Registering them like this:
Messenger.Default.Register<PhoneApplicationPage>(this, "NavigatedFrom", false, (action) => SaveState(action));
Messenger.Default.Register<PhoneApplicationPage>(this, "NavigatedTo", false, (action) => RestoreState(action));
Executing a command from code behind is far cleaner than going through the whole messaging mess. After all there's nothing wrong with the view knowing about its DataContext.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
viewModel.NavigatedToCommand.Execute(e.Uri);
}
ProfileViewModel viewModel
{
get
{
return this.DataContext as ProfileViewModel;
}
}
Update: Passing in NavigationContext.QueryString is probably more useful, since it already parses out the parameters and value.
Sorry for being three years late to this question. Yes, I'm still using Silverlight. Okay I want to write it in Page code-behind like this:
// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.HandleOnNavigatedTo(e);
}
I am using an extension method like this:
public static void HandleOnNavigatedTo(this Page page, NavigationEventArgs e)
{
var vm = page.DataContext as IPageNavigationViewModel;
if (vm == null) return;
vm.HandleOnNavigatedTo(e);
}
The extension method implies that the Page must have a View Model that implements IPageNavigationViewModel in DataContext. For me, this is a separation-of-concerns compromise where the Page knows only about the most general-purpose data types in the Domain. This the interface:
using System.Windows.Navigation;
namespace Fox.Silverlight.ViewModels
{
/// <summary>
/// Defines View Model members for frame-navigation pages.
/// </summary>
public interface IPageNavigationViewModel
{
/// <summary>
/// Handles the <see cref="Page.OnNavigatedTo"/> method in the View Model.
/// </summary>
/// <param name="e">The <see cref="NavigationEventArgs"/> instance containing the event data.</param>
void HandleOnNavigatedTo(NavigationEventArgs e);
/// <summary>
/// Handles the <see cref="Page.OnNavigatedFrom"/> method in the View Model.
/// </summary>
/// <param name="e">The <see cref="NavigationEventArgs"/> instance containing the event data.</param>
void HandleOnNavigatedFrom(NavigationEventArgs e);
}
}
Looks like you have a solution to your problem already. I would also suggest the following:
Look at using one of the message values provided in the mvvm-toolkit, such as:
NotificationMessage<T>
Like this:
Messenger.Default.Send<NotificationMessage<PhoneApplicationPage>>(
new NotificationMessage<PhoneApplicationPage>(this, "Message"));
I think what Ryan was getting at, was the fact that you're using the PhoneApplicationPage as the message that is being sent, instead of an actual message.
You're doing this:
Messenger.Default.Send<PhoneApplicationPage>(this);
which is sending a message of type PhoneApplicationPage. You probably don't need to send the entire PhoneApplicationPage as the message.
You could make some messages for NavigatingTo / NavigatingFrom, ie.
Messenger.Default.Send<NavigatingToMessage>(new NavigatingToMessage());
etc.
I'm sure there are a million better ways to do this, I was just going along with how you had set things up. Personally, my ViewModelBase class has NavigatingTo/NavigatingFrom methods and I override the respective methods in the View and send them to my ViewModel.
I make a sample using the updated answer inside the question :
MainViewModel.xaml.cs :
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
Messenger.Default.Register<PhoneApplicationPage>(this, "NavigatedTo", false, ExecuteNavigatedTo);
}
// action contains everything you want.
private void ExecuteNavigatedTo(Page page)
{
// example
bool b = page.NavigationContext.QueryString.ContainsKey("id");
}
}
MainViewModel.xaml.cs :
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Messenger.Default.Send<PhoneApplicationPage>(this, "NavigatedTo");
base.OnNavigatedTo(e);
}

GWT Custom Events

Hey I have a problem getting my head around how custom GWT event Handlers work. I have read quite a bit about the topic and it still is some what foggy. I have read threads here on Stackoverflow like this one GWT Custom Event Handler. Could someone explain it in an applied mannar such as the following.
I have 2 classes a block and a man class. When the man collides with the block the man fires an event ( onCollision() ) and then the block class listens for that event.
Thanks
Events in general:
Events are always sent to inform about something (e.g. a change of state). Let's take your example with a man and a wall. Here we can imagine that there is a game where a user can walk as a man in a labyrinth. Every time a user hits the wall it should be informed about the collision so that it can react to it (e.g. a wall can render itself as a destroyed wall). This can be achieved by sending a collision event every time the collision with a wall is detected. This event is sent by a man and every object in the system interested in the event receives it and can react to it accordingly. Objects which want to receive events must register themselves as interested with event.
This is how events work in general in every system or framework (not only in GWT). In order to send and receive events in such systems you have to define:
What is sent (what do events look like)
Who receives events (event receivers)
Who sends events (event senders)
Then you can:
Register event receivers which want to receive events
Send events
Events in GWT:
Here I will show an example of using custom events in GWT. I will use an example of a system which is responsible for checking a mailbox and inform a user if there are new mails. Let's assume that in the system there are at least 2 components:
message checker responsible for checking the mailbox and
message displayer responsible for displaying new mails
Message checker sends events when a new mail is received and message displayer receives these events.
Step 1: Define events
Information about a new mail will be sent as an instance of MessageReceivedEvent class. The class contains a new mail (for the simplicity let's assume it is just a String).
Full source code of this class is presented below (the comment for it is below the source code).
public class MessageReceivedEvent extends GwtEvent<MessageReceivedEventHandler> {
public static Type<MessageReceivedEventHandler> TYPE = new Type<MessageReceivedEventHandler>();
private final String message;
public MessageReceivedEvent(String message) {
this.message = message;
}
#Override
public Type<MessageReceivedEventHandler> getAssociatedType() {
return TYPE;
}
#Override
protected void dispatch(MessageReceivedEventHandler handler) {
handler.onMessageReceived(this);
}
public String getMessage() {
return message;
}
}
MessageReceivedEventHandler is an interface that represents event receivers. Don't bother with it at the moment, this will be discussed later.
Every class representing a GWT event has to extend GwtEvent class. This class contains two abstract methods which must be implemented: getAssociatedType and dispatch. However in every event class they are usually implemented in a very similar way.
The class stores information about a received message (see constructor). Every event receiver can get it using getMessage method.
Step 2: Define event receivers
Each event type in GWT is associated to an interface representing receivers of this event type. In GWT receivers are called handlers. In the example an event receiver interface for MessageReceivedEvent will be named MessageReceivedEventHandler. The source code is below:
public interface MessageReceivedEventHandler extends EventHandler {
void onMessageReceived(MessageReceivedEvent event);
}
Each handler has to extend EventHandler interface. It should also define a method which will be invoked when an event occurs (it should take at least one parameter - an event). Here the method is named onMessageReceived. Each receiver can react on an event by implementing this method.
The only event receiver in the example is MessageDisplayer component:
public class MessageDisplayer implements MessageReceivedEventHandler {
#Override
public void onMessageReceived(MessageReceivedEvent event) {
String newMessage = event.getMessage();
// display a new message
// ...
}
}
Step 3: Define event senders
In the example the only event sender is a component responsible for checking mails - EventChecker:
public class MessageChecker implements HasHandlers {
private HandlerManager handlerManager;
public MessageChecker() {
handlerManager = new HandlerManager(this);
}
#Override
public void fireEvent(GwtEvent<?> event) {
handlerManager.fireEvent(event);
}
public HandlerRegistration addMessageReceivedEventHandler(
MessageReceivedEventHandler handler) {
return handlerManager.addHandler(MessageReceivedEvent.TYPE, handler);
}
}
Every event sender has to implement HasHandlers interface.
The most important element here is a HandlerManager field. In GWT HandlerManager as the name suggest manages event handlers (event receivers). As it was said at the beginning every event receiver that wants to receive events must register itself as interested. This is what handler managers are for. They make it possible to register event handlers an they can send a particular event to every registered event handler.
When a HanlderManager is created it takes one argument in its constructor. Every event has a source of origin and this parameter will be used as a source for all events send by this handler manager. In the example it is this as the source of events is MessageChecker.
The method fireEvent is defined in HasHandlers interface and is responsible for sending events. As you can see it just uses a handler manager to send (fire) and event.
addMessageReceivedEventHandler is used by event receivers to register themselves as interested in receiving events. Again handler manager is used for this.
Step 4: Bind event receivers with event senders
When everything is defined event receivers must register themselves in event senders. This is usually done during creation of objects:
MessageChecker checker = new MessageChecker();
MessageDisplayer displayer = new MessageDisplayer();
checker.addMessageReceivedEventHandler(displayer);
Now all events sent by checker will be received by displayer.
Step 5: Send events
To send an event, MessageChecker must create an event instance and send it using fireEvent method. This cane be done in newMailReceived method:
public class MessageChecker implements HasHandlers {
// ... not important stuff omitted
public void newMailReceived() {
String mail = ""; // get a new mail from mailbox
MessageReceivedEvent event = new MessageReceivedEvent(mail);
fireEvent(event);
}
}
I hope it is clear and will help :)
Since this question and the answer from Piotr GWT has added support for a slightly different way to create custom events. This event implementation is specific build to be used with the GWT's EventBus in the package com.google.web.bindery.event.shared. An example on how to build a custom event for GWT 2.4:
import com.google.web.bindery.event.shared.Event;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.HandlerRegistration;
/**
* Here is a custom event. For comparison this is also a MessageReceivedEvent.
* This event extends the Event from the web.bindery package.
*/
public class MessageReceivedEvent extends Event<MessageReceivedEvent.Handler> {
/**
* Implemented by methods that handle MessageReceivedEvent events.
*/
public interface Handler {
/**
* Called when an {#link MessageReceivedEvent} event is fired.
* The name of this method is whatever you want it.
*
* #param event an {#link MessageReceivedEvent} instance
*/
void onMessageReceived(MessageReceivedEvent event);
}
private static final Type<MessageReceivedEvent.Handler> TYPE =
new Type<MessageReceivedEvent.Handler>();
/**
* Register a handler for MessageReceivedEvent events on the eventbus.
*
* #param eventBus the {#link EventBus}
* #param handler an {#link MessageReceivedEvent.Handler} instance
* #return an {#link HandlerRegistration} instance
*/
public static HandlerRegistration register(EventBus eventBus,
MessageReceivedEvent.Handler handler) {
return eventBus.addHandler(TYPE, handler);
}
private final String message;
public MessageReceivedEvent(String message) {
this.message = message;
}
#Override
public Type<MessageReceivedEvent.Handler> getAssociatedType() {
return TYPE;
}
public String getMessage() {
return message;
}
#Override
protected void dispatch(Handler handler) {
handler.onMessageReceived(this);
}
}
The event is used as follows:
To register your handler for this event with the eventbus call the static register method on the MessageReceivedEvent class:
MessageReceivedEvent.register(eventbus, new MessageReceivedEvent.Handler() {
public void onMessageReceived(MessageReceivedEvent event) {
//...do something usefull with the message: event.getMessage();
}
});
Now to fire the event on the eventbus call fireEvent with a newly constructed event:
eventBus.fireEvent(new MessageReceivedEvent("my message"));
Another implementation can be found in GWT's own EntityProxyChange event class. That implementation uses a alternative option of the EventBus. It uses the ability to add handlers that are bound to a specific source, via addHandlerToSource and can be triggered via eventBus.fireEventFromSource.
The event implementation given here is also more suitable when working with GWT's Activities.
I created my own widget by extending GWT's Composite class. I wanted to create my own custom event in this class. I wanted the events to be accessible to GWT's WindowBuilder Editor.
I learned a lot of from the answers on this page, but I had to make some changes.
I wanted to start from the Hilbrand Bouwkamp answer, because it was newer. But I ran into a couple of problems. 1) That answer made reference to the event bus. The even bus is a global variable owned by the main program. It's not clear how a widget library could get access to that. 2) I wasn't starting from scratch. I was was extending GWT library code. In order to make that work, I had to start from the GwtEvent class, rather than the Event class.
Piotr's answer is essentially correct, but it was very long. My class (indirectly) extends GWT's Widget class. Widget takes care of many details, such as creating a HandlerManager object. (I looked through the source code, and that's exactly how standard Widgets work, not by using an EventBus.)
I only had to add two things to my widget class to add a custom event handler. Those are shown here:
public class TrackBar extends Composite {
public HandlerRegistration addValueChangedHandler(TrackBarEvent.Handler handler)
{
return addHandler(handler, TrackBarEvent.TYPE);
}
private void fireValueChangedEvent(int value)
{
final TrackBarEvent e = new TrackBarEvent(value);
fireEvent(e);
}
My new event is almost exactly the same as Piotr's event class, shown above. One thing is worth noting. I started with getValue(), based on that example. Later I added getTrackBar() to give a lot more information. If I was starting from scratch I'd focus on the latter, not the former. The complete event class is shown below.
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
public class TrackBarEvent extends GwtEvent< TrackBarEvent.Handler >
{
public interface Handler extends EventHandler {
void onTrackBarValueChanged(TrackBarEvent event);
}
static final Type<TrackBarEvent.Handler> TYPE =
new Type<TrackBarEvent.Handler>();
private final int value;
public TrackBarEvent(int value) {
this.value = value;
}
#Override
public Type<TrackBarEvent.Handler> getAssociatedType() {
return TYPE;
}
public int getValue() {
return value;
}
public TrackBar getTrackBar()
{
return (TrackBar)getSource();
}
#Override
protected void dispatch(Handler handler) {
handler.onTrackBarValueChanged(this);
}
}
If you happen to be using the GWTP framework on top of GWT, refer to this Stack.
GWTP is "A complete model-view-presenter framework to simplify your next GWT project."

Is it possible to seamlessly display an ajax spinner for every GWT RPC call?

I have a GWT application that uses RPC calls heavily. I would like to display a spinner icon whenever a call is in progress. It is easy enough to display the icon, but I want to do it seamlessly in one place so I don't have to explicitly hide and show the icon for each call.
I guess I am looking for something similar to jQuery's ajaxStart and ajaxStop events.
Has anyone done something like this before?
Cheers
Tin
Why don't you implement this behaviour in a concrete implementation of AsyncCallback and subclass all the AsyncCallbacks from this one. Alternatively you could use a decorator pattern where you use a regular AsyncCallback and decorate it with another one that shows/hides the popup.
Alternatively, if you use a Command Pattern, you could just add these events to your command pattern implementation and you can register a handler that shows/hides a popup every time a request is send/received.
In response to comments that suggest a Decorator is not enough.
abstract class AbstractAsyncCallback <T> implements AsyncCallaback <T>
{
public AbstractAsyncCallback ()
{
Foo.showIcon();
}
#Override public void success (T t)
{
doSuccess(t);
Foo.hideIcon();
}
#Override public void failure ()
{
doFailure();
Foo.hideIcon();
}
public abstract void doSuccess (T t);
public abstract void doFailure (T t);
};

Resources