IMediator Not working on Silverlight - Help

Aug 26, 2010 at 10:38 AM

Hi

I have got and issue that is eating my time, IMediator is not working for some reason (I know what's the problem not not understand why this is happening)

My scenario is a simple. Vanilla Silverlight 4 with a view and viewmodel implementing the viewmodel from MEFedMVVM that is working fine and this viewmodel is also using IMediator so I got a line for

[Import(typeof(IMediator), RequiredCreationPolicy = CreationPolicy.Shared, AllowRecomposition = true)]
public IMediator Mediator

...

Another line for registering this viewmodel IMediator

_mediator = value;
_mediator.Register(this);

and my receptor on the same viewmodel

 [MediatorMessageSink("ContentFrameNavigated", ParameterType = typeof(Uri))]
public void OnSelectedUserChanged(Uri newPage)
{
        Uri = newPage;
}

and finally in my view i am raising the event on page NavigatedTo

protected override void OnNavigatedTo(NavigationEventArgs e)
{
      _mediator.NotifyColleagues("ContentFrameNavigated", e.Uri);
}

I think I got everything that I need but the receptor never got called.

So after much investigation I found out that InvocationList was empty fro the raiser point of view but had an item after I registered, so did more investigation and I found out even when Mediator is been exported as Shared my to imports of IMediator (One from the view one from the view model) are creating to instances of Mediator.

That's is the problem as you can see MEF is not behaving as it should, Is MEF broken or I am missing something

 

BTW I Have also tried recomposition

             /var c = new TypeCatalog(typeof(ModuleContainer));
            //var catalog = new AggregateCatalog(c);
            //var container = new CompositionContainer(catalog);
            //// adding new catalog (will result in recomposition)
            //catalog.Catalogs.Add(new TypeCatalog(typeof(IMediator)));
            //CompositionInitializer.SatisfyImports(this);

Coordinator
Aug 26, 2010 at 10:43 AM
never saw such a behaviour! How are you creating your ViewModels? 

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Aug 26, 2010 at 10:38 AM, acosta <notifications@codeplex.com> wrote:

From: acosta

Hi

I have got and issue that is eating my time, IMediator is not working for some reason (I know what's the problem not not understand why this is happening)

My scenario is a simple. Vanilla Silverlight 4 with a view and viewmodel implementing the viewmodel from MEFedMVVM that is working fine and this viewmodel is also using IMediator so I got a line for

[Import(typeof(IMediator), RequiredCreationPolicy = CreationPolicy.Shared, AllowRecomposition = true)]
public IMediator Mediator

...

Another line for registering this viewmodel IMediator

_mediator = value;
_mediator.Register(this);

and my receptor on the same viewmodel

 [MediatorMessageSink("ContentFrameNavigated", ParameterType = typeof(Uri))]
public void OnSelectedUserChanged(Uri newPage)
{
        Uri = newPage;
}

and finally in my view i am raising the event on page NavigatedTo

protected override void OnNavigatedTo(NavigationEventArgs e)
{
      _mediator.NotifyColleagues("ContentFrameNavigated", e.Uri);
}

I think I got everything that I need but the receptor never got called.

So after much investigation I found out that InvocationList was empty fro the raiser point of view but had an item after I registered, so did more investigation and I found out even when Mediator is been exported as Shared my to imports of IMediator (One from the view one from the view model) are creating to instances of Mediator.

That's is the problem as you can see MEF is not behaving as it should, Is MEF broken or I am missing something

 

BTW I Have also tried recomposition

             /var c = new TypeCatalog(typeof(ModuleContainer));
            //var catalog = new AggregateCatalog(c);
            //var container = new CompositionContainer(catalog);
            //// adding new catalog (will result in recomposition)
            //catalog.Catalogs.Add(new TypeCatalog(typeof(IMediator)));
            //CompositionInitializer.SatisfyImports(this);

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Aug 26, 2010 at 10:49 AM

Thanks for the quick answer

 

The most standard way

ViewModel

[ExportViewModel("MyClassViewModel")]
public class MyClassViewModel

 

View

xmlns:meffed="http:\\www.codeplex.com\MEFedMVVM"
meffed:ViewModelLocator.SharedViewModel="MyClassViewModel">

an calling CompositionInitializer.SatisfyImports(this); from view constructor to force my Mediator to be imported

 

 

Coordinator
Aug 26, 2010 at 10:54 AM
"an calling CompositionInitializer.SatisfyImports(this); from view constructor to force my Mediator to be imported"

so you want the Mediator in the View as well?

just to clarify are you trying to send a message from the ViewModel to the View?

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Aug 26, 2010 at 10:49 AM, acosta <notifications@codeplex.com> wrote:

From: acosta

Thanks for the quick answer

 

The most standard way

ViewModel

[ExportViewModel("MyClassViewModel")]
public class MyClassViewModel

 

View

xmlns:meffed="http:\\www.codeplex.com\MEFedMVVM"
meffed:ViewModelLocator.SharedViewModel="MyClassViewModel">

an calling CompositionInitializer.SatisfyImports(this); from view constructor to force my Mediator to be imported

 

 

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Aug 26, 2010 at 10:57 AM

I want my view model to know where OnNavigatedTo is fired so I can handle Navigation I also try from the main page hooking the mediator to the main navigation event and had the same issue.

Coordinator
Aug 26, 2010 at 11:00 AM
try this in the View

 Loaded += delegate { ((RatesViewModel) DataContext).Mediator.Register(this); };


Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Aug 26, 2010 at 10:57 AM, acosta <notifications@codeplex.com> wrote:

From: acosta

I want my view model to know where OnNavigatedTo is fired so I can handle Navigation I also try from the main page hooking the mediator to the main navigation event and had the same issue.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Aug 26, 2010 at 11:03 AM

Actually that works but is dirty I am not a purist MVVM but try to get code behind clean and the most important this I need to understand why Mediator exported as Shared is been created twice.

Coordinator
Aug 26, 2010 at 11:06 AM
the problem is that MEFedMVVM has its own Composition container, so CompositionInitializer will not be using the MEFedMVVM one... Ideally you should not having the View receiving mediator messages... 


Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Aug 26, 2010 at 11:03 AM, acosta <notifications@codeplex.com> wrote:

From: acosta

Actually that works but is dirty I am not a purist MVVM but try to get code behind clean and the most important this I need to understand why Mediator exported as Shared is been created twice.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Aug 26, 2010 at 11:50 AM

Can you not using AggregateCatalog to merge those ?

that is a quite big limitation because the are are event happening on the view that you may want to know, navigation been one of those.

Coordinator
Aug 26, 2010 at 11:56 AM
usually when you are doing MVVM you would have an EventToCommand that would execute a command on the ViewModel. Code behind should only be there for UI ONLY specific things

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Aug 26, 2010 at 11:50 AM, acosta <notifications@codeplex.com> wrote:

From: acosta

Can you not using AggregateCatalog to merge those ?

that is a quite big limitation because the are are event happening on the view that you may want to know, navigation been one of those.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Aug 26, 2010 at 11:58 AM

Do you have something is the framework to managed this EventToCommand? can you give and small example to have and idea  please?

Coordinator
Aug 26, 2010 at 12:03 PM
have a look at 


or even you can get it from MVVMLight
http://www.galasoft.ch/mvvm/getstarted/

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Aug 26, 2010 at 11:58 AM, acosta <notifications@codeplex.com> wrote:

From: acosta

Do you have something is the framework to managed this EventToCommand? can you give and small example to have and idea  please?

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Aug 26, 2010 at 12:16 PM

Going back to the idea of MEFedMVVM "having its own Composition container" that potencially force us to use this container in favor over MEF other wise we wont be able to use MEF and MEFedMVVM is the same project to share object as the whole issue with IMediator shows, is this possible to fix. I am asking because this make MEF not much extensible.

Coordinator
Aug 26, 2010 at 12:21 PM
you can get hold of MEFedMVVM composition container.... 

do this
ViewModelRepoitory.Instance.Resolver.Container.GetExportedValue<IMediator>();

STUPID ME!!! Sorry dude... should have suggested this before... sorry again... my brain is not working today !


Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Aug 26, 2010 at 12:17 PM, acosta <notifications@codeplex.com> wrote:

From: acosta

Going back to the idea of MEFedMVVM "having its own Composition container" that potencially force us to use this container in favor over MEF other wise we wont be able to use MEF and MEFedMVVM is the same project to share object as the whole issue with IMediator shows, is this possible to fix. I am asking because this make MEF not much extensible.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Coordinator
Aug 26, 2010 at 12:22 PM
yet again... I suggest you look at Event to command.... makes things nicer and decreases your codebehind

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Aug 26, 2010 at 12:21 PM, Marlon Grech <marlongrech@gmail.com> wrote:
you can get hold of MEFedMVVM composition container.... 

do this
ViewModelRepoitory.Instance.Resolver.Container.GetExportedValue<IMediator>();

STUPID ME!!! Sorry dude... should have suggested this before... sorry again... my brain is not working today !


Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Aug 26, 2010 at 12:17 PM, acosta <notifications@codeplex.com> wrote:

From: acosta

Going back to the idea of MEFedMVVM "having its own Composition container" that potencially force us to use this container in favor over MEF other wise we wont be able to use MEF and MEFedMVVM is the same project to share object as the whole issue with IMediator shows, is this possible to fix. I am asking because this make MEF not much extensible.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com



Aug 26, 2010 at 1:08 PM

That's something that I am going to use, thanks

Aug 26, 2010 at 1:09 PM
Can you give some more details on that please ? how can you get hoold of ViewModelRepoitory.Instance.Resolver.Container.GetExportedValue<IMediator>();? Shall I use and import ?

marlongrech wrote:
you can get hold of MEFedMVVM composition container.... 
do this
ViewModelRepoitory.Instance.Resolver.Container.GetExportedValue<IMediator>();
STUPID ME!!! Sorry dude... should have suggested this before... sorry again... my brain is not working today !
Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Aug 26, 2010 at 12:17 PM, acosta <notifications@codeplex.com> wrote:

From: acosta

Going back to the idea of MEFedMVVM "having its own Composition container" that potencially force us to use this container in favor over MEF other wise we wont be able to use MEF and MEFedMVVM is the same project to share object as the whole issue with IMediator shows, is this possible to fix. I am asking because this make MEF not much extensible.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


 

Aug 26, 2010 at 1:14 PM

I got it working did not see the Instance was static.

 

Aug 26, 2010 at 1:22 PM

Now that I got two options

Option 1

MEFedMVVM.ViewModelLocator.ViewModelRepository.Instance.Resolver.Container.GetExportedValue<IMediator>();

Option 2

Event to command (Still not sure which one to use) Do you know if MVVMLight uses Unity to get that working or it's 100% SL Behaviours ?

 

Now putting that outside I am still got issue with MEFedMVVM having it's own Composition container, yesterday I sent you and email about having two SL project one being the shell that loads the Modules(USING MEFedMVVM) on demand using MEF (NOT USING MEFedMVVM at all) and also failing  on a similar fashion and the attached property on the view not been fired, Is there anyway that we can merger the your container with MEF default container ?

 

Coordinator
Aug 26, 2010 at 1:32 PM

MVVM light is 100% behavior no Unity.

with regards to the sample I got your mail and I did not have time to do it... this is totally doable since I did it a couple of times, the trick is to create an IComposer and then tell MEFedMVVM to use that composer via LocatorBootstrapper.ApplyComposer(new MyComposer());

did you see this post?

http://marlongrech.wordpress.com/2010/04/27/how-does-mefedmvvm-compose-the-catalogs-and-how-can-i-override-the-behavior-mefedmvvm-part-4/

 

Aug 26, 2010 at 1:50 PM
marlongrech wrote:

MVVM light is 100% behavior no Unity.

with regards to the sample I got your mail and I did not have time to do it... this is totally doable since I did it a couple of times, the trick is to create an IComposer and then tell MEFedMVVM to use that composer via LocatorBootstrapper.ApplyComposer(new MyComposer());

did you see this post?

http://marlongrech.wordpress.com/2010/04/27/how-does-mefedmvvm-compose-the-catalogs-and-how-can-i-override-the-behavior-mefedmvvm-part-4/

 

So do you think having different container is what is making the attached property to fail?

Coordinator
Aug 26, 2010 at 3:04 PM
I will look at the project you sent but unless you tell the IComposer what XAP need downloading then they will not be part of the main composition... there is a sample that shows multiple xaps did you look at it?

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Aug 26, 2010 at 1:50 PM, acosta <notifications@codeplex.com> wrote:

From: acosta

marlongrech wrote:

MVVM light is 100% behavior no Unity.

with regards to the sample I got your mail and I did not have time to do it... this is totally doable since I did it a couple of times, the trick is to create an IComposer and then tell MEFedMVVM to use that composer via LocatorBootstrapper.ApplyComposer(new MyComposer());

did you see this post?

http://marlongrech.wordpress.com/2010/04/27/how-does-mefedmvvm-compose-the-catalogs-and-how-can-i-override-the-behavior-mefedmvvm-part-4/

 

So do you think having different container is what is making the attached property to fail?

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Aug 26, 2010 at 4:50 PM

Yes I saw your sample however I need to download the files on demand, so in the sample I sent you you will see that action happen on navigation.

Aug 26, 2010 at 5:19 PM

BTW I found that the sample is not implementing the whole  IComposer interface (MyMEFedMVVMComposer.cs) is missing GetCustomExportProviders to be implemented, What we need to add in there?

Coordinator
Aug 26, 2010 at 5:26 PM
do a get latest because I think I fixed that... but you return null if you do not want to implement your own ExportProviders 

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Aug 26, 2010 at 5:19 PM, acosta <notifications@codeplex.com> wrote:

From: acosta

BTW I found that the sample is not implementing the whole  IComposer interface (MyMEFedMVVMComposer.cs) is missing GetCustomExportProviders to be implemented, What we need to add in there?

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Aug 26, 2010 at 5:53 PM

I got the version from yesterday. so I the Interface still got the two properties.

 

 

 

Aug 26, 2010 at 6:01 PM

I have created a new discussion about the implementation of IComposer and IDeploymentCatalogService, can we park this discussion  as it is.