Thursday, 2 August 2012

Post/Email-3: Events in .NET

Today, a friend of mine asked a question on facebook:
Any body knows why wouldn't the selectionChanged event be fired?
I'm trying to change the text of a textBlock once I scroll to the next item in a pivot page
When I came into the scene, so to speak, another person had supplied the fix for the issue like so:
hmm in xaml u have to call your function SelectionChanged="thisPivot_SelectionChanged" and your textblock gets changed...

Then I started talking to my friend, and here’s how our conversation went:

Saturday, 9 June 2012

Post/Email-2: Views, ViewModels and Composition

Recently a friend of mine asked me the following question on Facebook:

brother, hope i am not disturbing you. had a question. if i have mutliple tab items in one page in y silverlight app, while each tab serves different functionalities, would it be all right if i write separate view models for all of the tab items, and i expose this view models as properties from one view model and bind the page's datacontext to that main viewmodel?

Thursday, 7 June 2012

Post/Email–1: MVVM Light messaging, order of register and send is important

Recently a friend of mine asked me the following question on Facebook. I started writing a few sentences, which turned into a few paragraphs and before I knew it I was writing a very long reply. Then, something Scott Hanselman said on twitter came to mind:
"Every email you send is a blog post you didn't write. Stop writing emails.”
Because of this, I intend to write these “Post/Email” (or “Post over Email”) blog posts whenever someone asks me a question, so that other people would benefit from them :)
Here we go, with this question from my friend Saad Galib on facebook: it at all possible to pass data between ViewModels using MVVM light messaging without ViewModelLocator? Because in a blog i read that that some class needs to register for a message before it is sent to receive it. Right now i have no locator. i send a message from ViewModel-1 bound to page 1 and then navigate to page 2 which in turn creates viewmodel 2 and in that viewmodel's constructor i register for that kind of messages(broadcasted from viewmodel1)....but its not working....:(
and this i definitely not satisfying the condition i read as i mentioned above. is there any work around?
The short answer is: no. The "no" here isn't because of the ViewModelLocator itself, but rather because of what you mentioned later about the order in which the subscription and broadcast are done.
The Messenger implementation in MVVM Light is simple and straightforward. When you register an object to some event, it adds it to a dictionary; and when that particular event is sent, it goes through the corresponding objects in the dictionary and notifies them (by calling the corresponding registered action). Therefore, when an object subscribes to a certain event, it only gets notified of those instances of the event which occur after it has subscribed. Which is the logical thing to do.
I'm not aware of anything in MVVM Light that would help with your particular situation, but there are other, maybe not so elegant, methods to transmit data between objects in a loosely coupled way. One of them, which I used myself but I'm not sure whether it's a good practice or not, is to have some sort of "buffer" where an object can put some data for other objects to take out at some point in time (in my particular scenario, I used a class "Pipe" with a static member "Data" that I set in one VM and consume in the other).
Hope this helps

Tuesday, 21 February 2012

What are Generics in .NET?

A little while ago, a friend of mine on Facebook asked me to explain to him what generics in .NET were. As I thought this might interest more than one friend, which it did, I decided to put it in a "note" on Facebook. That was before I started this blog, so now I decided to post it here, hopefully so that more people would find it useful.

Generics in the .NET Framework is a programming technique that allows you to create a "generic" (meaning that it's not tied to a specific type) class, structure or method. The type, which is in this case "an argument", is specified when that element is used. What's interesting, and this is another advantage of Generics in .NET, is that you can restrict that type by specifying, for example, that it has to implement a certain interface or have a parameterless constructor.

This concept allows for more type safety (the type is known when the generic element is being used), while promoting reuse (you create an element that's not tied to a specific type).

Sunday, 19 February 2012

Is Silverlight Dead?

"To Silverlight or Not to Silverlight!" That my dear reader is a question I've grown accustomed to hear! Here's part of a little chat I had with a friend on Messenger a couple of days ago:
"Friend" says:
yes that's exactly what i meant !
btw , is Silverlight dying?
"Me" says:
nah, it's having a headache that’s all
(from all the people who are saying it's dying )
"Friend" says:
haha, i see