Messaging Centre - Subscribing Event in the ViewModel Constructor - xamarin.forms

I am trying to send data from one ViewModel to another using Messaging Centre.
I have subscribed the event in 2nd ViewModel's constructor. But the event is not subscribed as the constructor is not compiled until I open the page/view corresponding to the ViewModel.
I am using MVVM Light, until now I had an understanding that the VM's constructor are compiled when ViewModelLocator is called at the app startup.
Can someone help me understand this better and how can I subscribe the event i.e. compile the constructor without the VM being called.

Perhaps you are thinking about this in the wrong way. Without seeing code it's difficult to see what you are trying to achieve exactly, but what you could do is subscribe to the event elsewhere in your app, for example in your App.xaml.cs. When the event fires, at that point navigate to a new page of type ViewModel2 and pass any details required as a navigation parameter.

Related

How to perform web service call after updating a content part in Orchard?

I have a Web API service that I have managed to use to pull values into a ContentPart by using the method outlined here: How to change Orchard record repository.
Now, what I want to do is update the values of the properties on my ContentPart and have the update performed via a Web API call. I think I'm close, but just can't get an update method to fire.
In my Handler, I have added a handler for OnUpdate. I can see my OnUpdate method get registered, but it never executes.
So my question is: Is this the correct handler event to use? And if so, how can I get it to trigger?
I should add that I am accessing the ContentPart through a Controller, as opposed to a Driver.
OnUpdating / OnUpdated get fired whenever you call IContentManager.UpdateEditor(item). In the default scenario this happens when you hit "Save" button when editing your content item.
I don't quite get what you mean by "accessing the ContentPart through a Controller"?
Do you have a custom controller that handles the item editor rendering and it's postback?
Or are you creating and updating some items in code, without using the built-in editors at all?
In the former case, you need to ensure that
IContentEditor.UpdateEditor(item) gets called for the whole content
item inside the POST action (same way it does in the default
controller - Core\Contents\Controllers\AdminController.cs).
In the latter, which I guess might be the case here, OnUpdating /
OnUpdated won't be fired and you need to call the web service on
your own from the controller action, as Bertrand pointed out in the comments.
There is also a third option available and I found it particularly useful in similar cases:
Use LazyField<T> as a backing field for those part properties you need to push to web service after update.
Put the code that calls web service inside that lazy field's setter (set this up during handler OnActivated event).
Now, whenever your property gets updated, a call to web service will be made, ie. the lazy field will act a a transparent proxy between your web service and current code.
For examples how to work with lazy fields take a look into CommonPartHandler class and methods LazyLoadHandlers and PropertySetHandlers.

Flex Events flow in non hierarchy e.g via framework responder

say an application has panel and panel has button and textField or textArea. we click on a button to hit a service say via cairngorm framework.
The Events lifecycle follows the capture-target-bubble cycle in the display hierarchy like the MouseClick event on button will call the handlers for the listeners attached to button and/or panel and/or application and/or stage since the flow moves in this direction.
How do the event on non hierarchy works e.g the result from the service fires a custom event DataRecieved. we have a listener attached to the panel/button for this dataRecieved event. How do this listener gets the information? how the event lifecycle comes into this picture?
Can anyone explain this?
Thanks!
Since it's a non-DisplayObject, the Event only has a target phase. That means that only listeners attached to the Object which is identical to the Object in the target property of the Event work. Hence, non-DisplayObject's can only dispatch Events to themselves. At least that's how I understand it.
In answer to a related question there is some discussion of writing custom EventDispatcher's that would enable a bubbling for non-DisplayObject's.
If you dispatch a custom event on a Flex UIComponent via dispatchEvent, the UIComponent takes care of bubbling the event through the display hierarchy, as long as the bubbles flag in the Event object is true.
I found out the concept of GlobalEventDispatcher to which we can attach listeners and dispatch from the same.
a ref from another question was helpful - Flex Custom Event dispatching
The important point is that - the instance of dispatcher must be same. we can keep a Custom Global singleton event dispatcher for our application or we can use stage/systemManager/FexGlobals.toplevelapplicaton for the purpose which might not be a good Practice.

MVVM light toolkit messenger problems

I'm using your messenger class to communicate between views/controls and viewmodels.
Currently I have the same usercontrol multiple times on the same view.
The problem is that when one viewmodel sends a message back to the usercontrol then all of the
usercontrols (of that same type) in my view get updated.
How can this be prevented?
FYI, I played around with the token functionality that you provide, but I couldn't make it work for my particular case.
Have you tried altering your usercontrol so that upon construction, you can specify whether or not it should Register for that particular message? I would do that as a starting point.
Another thing to watch out with using Messenger.Default -- it isn't threadsafe, so if you end up using it in lots of places other than usercontrols (like in worker threads to notify the main thread of events happening), then you had better wrap it in another class that performs the requisite locking.
You could set the target of the message if your message inherits from MessageBase class,
or use a Guid as a Messenger token.

WP7 - Why are my bindings not firing when the windows back button is pressed?

Hopefully there is an obvious answer to this that my brain at 2am is not seeing (I'm basing this on the fact I can't find anyone else with the same problem).
When I press the "hardware/built in" back button on my windows phone 7 none of my bindings are re-evaluating. For example on my MainPage I have a button's "IsEnabled" bound to a bool property in my ViewModel that checks if a value is in isolated storage. In the emulator it starts as disabled (obviously). I then navigate to another page and add the required value to isolated storage. When i press the hardware back button to return to the original page the binding does not get re-evaluated and my button remains disabled.
However if I go back to MainPage via code using the navigate method the binding gets re-evaluated and everything is good.
Any ideas as to why?
The issue sounds like the RaisePropertyChanged method isn't being raised by your MVVM Light ViewModel - if you're binding Get{} accessor of your IsEnabled property on the viewmodel directly to a value in IsolatedStorage, then the ViewModel isn't going to know that that underlying value has necessarily changed.
When you write the value to IsolatedStorage, you can use the Messenger interface in MVVM to notify the ViewModel-in-question that it should fire the RaisePropertyChanged() event for your model, and that will re-bind that property in your view.
When you use the NavigationService.Navigate method, you are actually performing a forwards navigation and a new instance of your page is created. When you press the hardware back button, a backwards navigation is performed and the previous page is re-displayed. Almost all of the time, what you get is a cached version of the original instance of the page.
You could re-evaluate any bindings in the OnNavigatedTo override for the page, but this introduces a fair bit of 'code smell'. As mentioned above, using the Messenger will enable you to update the associated view model, which will update the binding on the page.

Do I need to unsubscribe from (manually subscribed to) events in asp.net?

Do the same best practis rules regarding subscribing/unsubscribing to events apply in asp.net?
I know it might seem like a silly question, but when I think about it, I have never really seen any code where people first subscribe to an event on a page and then unsubscribe later on in the web request.
Example 1:
On a page, in the Page_Load method, I subscribe to a updating event on a ListView. Should I unsubscribe from that event later on, for example in the OnPreRenderComplete method?
Example 2:
In the passive view patter, a view (Page control/Usercontrol) will raise an event whenever it needs the presenter to do anything. So the presenter needs to subscribe to events on the view, but do it also need to unsubscribe from the events again?
Best regards, Egil.
The page instance and all of its components will "go out of scope" when request completes, e.g. they become eligible for GC. So your ListView will go out of scope along with the Page/user controls on it. You don't need to unsubscribe (unless you subscribe to an event that belongs to some kind of singleton that survives every request and use one of the methods of the page as the event handler, for example).
The same thing is valid for the presenter (again as long as this presenter is used solely with one page and goes out of scope after that).
Generally, no. Events are supposed to be dumped automatically when the page unloads. SUPPOSED to be. I've run into a bug before (in .NET 1.1) where that wasn't the case.
I won't bother unsubscribing, unless I notice a problem with the page (like, a method being called 20 times from a phantom in the call stack: that's usually a sign of something not being unsubscribed properly).

Resources