trigger an event from class library to wpf - event-handling

I have create a class library to handle to do payments by payment terminal.
I want to trigger an event to my wpf application, when the payment finish.
How can i achieve this?

I solved the above issue by using the delegates.
In Class library
1. Declare a delegate in classlibrary
public delegate void GetResponseDelegate(bool isSuccess);
Declare a delegate event in the class which you used to get the result
public event GetResponseDelegate responseEvent;
Call the delegate event within the method where you can get result
bool myResult=true;
if(responseEvent!=null)
{
responseEvent(myResult);
}
In wpf application
1. creating a object to the class library class where you declare the delegate event
MyLib.Class1 c1=new MyLib.Class1();
initialize the delegate event
c1.responseEvent+=new MyLib.GetResponseDelegate(GetResponseMethod);
3.Get your response inside the delegate event method
void GetResponseMethod(bool isSuccess)
{
//do your actions with the result
}

Related

Subscribe to propertyChanged event PostSharp

I have my ViewModel made with PostSharp:
[NotifyPropertyChanged]
class ProfileSelectorViewModel
{
public int Selection { get; set; }
}
Selection is bound to the selection property of a listbox. How can I subscribe to the change of this property? I want to call a method when Selection changes it's value.
You can subscribe to the PropertyChanged event by casting an object of ProfileSelectorViewModel class. Because your tooling may complain that the class does not implement the interface INotifyPropertyChanged yet, you can use a helper method Post.Cast<SourceType, TargetType>(SourceType).
So if you have an object obj of type ProfileSelectorViewModel and a method OnSelectionChanged handling the change, the subscription looks like this:
Post.Cast<ProfileSelectorViewModel,INotifyPropertyChanged>(obj).PropertyChanged += OnSelectionChanged;
See http://doc.postsharp.net/inotifypropertychanged-add#consuming for details.

GWT Editor framework

Is there a way to get the proxy that editor is editing?
The normal workflow would be:
public class Class implments Editor<Proxy>{
#Path("")
#UiField AntoherClass subeditor;
void someMethod(){
Proxy proxy = request.create(Proxy.class);
driver.save(proxy);
driver.edit(proxy,request);
}
}
Now if i got a subeditor of the same proxy
public class AntoherClass implements Editor<Proxy>{
someMethod(){
// method to get the editing proxy ?
}
}
Yes i know i can just set the proxy to the Child editor with setProxy() after its creation, but i want to know if there is something like HasRequestContext but for the edited proxy.
This usefull when you use for example ListEditor in non UI objects.
Thank you.
Two ways you can get a reference to the object that a given editor is working on. First, some simple data and a simple editor:
public class MyModel {
//sub properties...
}
public class MyModelEditor implements Editor<MyModel> {
// subproperty editors...
}
First: Instead of implementing Editor, we can pick another interface that also extends Editor, but allows sub-editors (LeafValueEditor does not allow sub-editors). Lets try ValueAwareEditor:
public class MyModelEditor2 implements ValueAwareEditor<MyModel> {
// subproperty editors...
// ValueAwareEditor methods:
public void setValue(MyModel value) {
// This will be called automatically with the current value when
// driver.edit is called.
}
public void flush() {
// If you were going to make any changes, do them here, this is called
// when the driver flushes.
}
public void onPropertyChange(String... paths) {
// Probably not needed in your case, but allows for some notification
// when subproperties are changed - mostly used by RequestFactory so far.
}
public void setDelegate(EditorDelegate<MyModel> delegate) {
// grants access to the delegate, so the property change events can
// be requested, among other things. Probably not needed either.
}
}
This requires that you implement the various methods as in the example above, but the main one you are interested in will be setValue. You do not need to invoke these yourself, they will be called by the driver and its delegates. The flush method is also good to use if you plan to make changes to the object - making those changes before flush will mean that you are modifying the object outside of the expected driver lifecycle - not the end of the world, but might surprise you later.
Second: Use a SimpleEditor sub-editor:
public class MyModelEditor2 implements ValueAwareEditor<MyModel> {
// subproperty editors...
// one extra sub-property:
#Path("")//bound to the MyModel itself
SimpleEditor self = SimpleEditor.of();
//...
}
Using this, you can call self.getValue() to read out what the current value is.
Edit: Looking at the AnotherEditor you've implemented, it looks like you are starting to make something like the GWT class SimpleEditor, though you might want other sub-editors as well:
Now if i got a subeditor of the same proxy
public class AntoherClass implements Editor<Proxy>{
someMethod(){
// method to get the editing proxy ?
}
}
This sub-editor could implement ValueAwareEditor<Proxy> instead of Editor<Proxy>, and be guaranteed that its setValue method would be called with the Proxy instance when editing starts.
In your child editor class, you can just implement another interface TakesValue, you can get the editing proxy in the setValue method.
ValueAwareEditor works too, but has all those extra method you don't really need.
This is the only solution I found. It involves calling the context edit before you call the driver edit. Then you have the proxy to manipulate later.

GWT - binding activityMapper with GIN not working

I´m trying to do my first steps with GWT/GIN.
I´ve downloaded the hellomvp example from google and followed this tutorial to get started with gin.
My problem is about this line in the configure-method of the HelloGinModule-class:
bind(ActivityMapper.class).to(AppActivityMapper.class).in(Singleton.class);
In my point of view it should bind my class "AppActivityMapper" as the active ActityManager.
But in fact the class constructor (or any method of the class) is never called, so the fired events are not caught.
The class AppActivityMapper looks like this:
public class AppActivityMapper implements ActivityMapper {
Provider<HelloActivity> helloActivityProvider;
Provider<GoodbyeActivity> goodbyeActivityProvider;
#Inject
public AppActivityMapper(final Provider<HelloActivity> helloActivityProvider, final Provider<GoodbyeActivity> goodbyeActivityProvider) {
this.helloActivityProvider = helloActivityProvider;
this.goodbyeActivityProvider = goodbyeActivityProvider;
}
#Override
public Activity getActivity(Place place) {
if (place instanceof HelloPlace) {
return helloActivityProvider.get();
} else if (place instanceof GoodbyePlace) {
return goodbyeActivityProvider.get();
}
return null;
}
}
In my example this code from my View-Class is called after clicking on a link:
presenter.goTo(new GoodbyePlace(name));
The event is fired to the event bus. But nothing happens.
Thanks in advance
You have defined an activity mapper somewhere in you GIN. But activity mapper have to be used in activity manager. Where do you create activity manager which will use your AppActivityMapper?
UPDATE:
The most logical thing is to keep activity manager out of the gin. E.g. in your ginjector you will have a method:
interface MyInjector extends Ginjector {
... //other methods
ActivityMapper getActivityMapper();
}
Than , when you create ginjector instance, you can create a manager and put correct activity mapper into it. for example:
MyInjector injector = GWT.create(MyInjector.class);
ActivityManager manager = new ActivityManager(injector.getActivityMapper(), injector.getEventBus());
If you have multiple managers and mappers, may be it will be better to extend ActivityManager class (so you can inject stuff into its constructor). Another solution is to use #Provides to initialize ActivityManager.

autofac instance initialization notification

Is there a way to determine when Autofac has completed an initialization of an instance?
You may need it if you have Lazy dependencies, or you inject dependencies via properties.
Possible solution might look like this:
public class Component : IKeepMeInformed {
private readonly IOtherComponent otherComponent;
public class Component(Lazy<IOtherComponent> otherComponent) {
this.otherComponent = otherComponent;
}
void IKeepMeInformed.InitializationCompleted() {
// Do whatever you need with this.otherComponent.Value
}
}
Not directly tied to Lazy components, but Autofac exposes events that lets you hook into the lifetime of instances. Listening for the OnActivated event will enable you to do stuff immediately after an instance have been created. E.g.:
builder.RegisterType<OtherComponentImplementation>().As<IOtherComponent>()
.OnActivated(e => InitializationCompleted(e.Instance));
Update: actually, in the context of your Component class, you should "know" when the instance is initialized. It will be whenever you access the Lazy<>.Value property first.

MVC ControllerActionInvoker and invoking actions

Is it possible to use a custom action invoker without having to instantiate it in the Controller handler factory? For example in custom controller factory:
IController IControllerFactory.CreateController(RequestContext reqContext, string controllerName)
{
var controller = base.CreateCOntroller(reqContext,controllerName ) as Controller;
controller.ActionInvoker = new CustomActionInvoker();
}
Or is there another way I can execute an MVC action without having to use a custom action invoker?
Updating the question
I have a controller, say HomeController and Index action. Index is the main action in the controller. Once the Index action gets executed, the MVC view will fire multiple actions using Ajax - GET requests (we using jTemplates).
Example
// Controller actions
// main action and View
public ActionResult Index() { ... }
public ActionResult AjaxAction1(string id) { ... }
public ActionResult AjaxAction2() { ... }
public ActionResult AjaxAction3() { ... }
Now I want to filter some of these actions not to execute depending on certain scenarios. For example I want to stop executing AjaxAction1 when the id is equal to 2.
Back to my original question. Is there a way to achieve this without using the action invoker. The reason that i don't want to use the action invoker is the way my project being structured ended up with circular references.
Any ideas greatly appreciated.
Found the answer you can subclass the Controller and create the ControllerActionInvoker there.
Using action method selector
Depending on what happens when id equals 2, but this could quite easily be done by writing a custom action method selector.
Using action method selector you could provide your actions that would execute depending on your parameter values:
[RequiresParameterValue("id", #"^2$")]
[ActionName("AjaxAction")]
public ActionResult AjaxAction1(string id) { ... }
[RequiresParameterValue("id", #"^[^2]*$")]
[ActionName("AjaxAction")]
public ActionResult AjaxAction2(string id) { ... }
As you can see from this example the custom action method selector takes two parameters:
controller action parameter name (id in example)
regular expression that checks controller action parameter value
Action method selector sees all route values as strings when they're coming from the client so you can actually pull this off by using regular expressions. And it makes it also very very flexible.
The first action method will get executed when id == "2" and the second one when id != "2".