IEventAggregator between views in the same module

Topics: Prism v4 - Silverlight 4
Feb 4, 2011 at 8:26 PM

Dear All,

I created an Interface to define my service data handling, then I implemented this interface in a seperate class dataservice.cs.

In my viewmodel I am importing both the eventAggregator & the service interface with constructor import, but my viewmodel doesn't get any of them.

My dataservice is marked with [Export(typeof(ICurrencyDataService))]

and my viewmodel constructor is defined as follows:

[ImportingConstructor]
public CurrencyViewModel(ICurrencyDataService dataService, IEventAggregator eventAggregator)
But it doesn't get it ... what am I doing wrong here ..!!

Feb 4, 2011 at 8:44 PM

Never mind I added the 

[PartCreationPolicy(CreationPolicy.Shared)]

 

And it works,

Thanks

Feb 4, 2011 at 9:22 PM

Now my page doesn't load ...

Here is the full story: (I followed the QuickStart UIComposition approach)

The DataService Class is populating data through a RIA service, whihc works fine and I can get the data into my viewmodel which defines as follows:

	[Export]
	public class CurrencyViewModel : NotificationObject
	{
		private IEventAggregator EventAggregator { get; set; }
		private ICurrencyDataService DataService { get; set; }

		public ICollectionView CurrencyItems { get; set; }

		[ImportingConstructor]
		public CurrencyViewModel(ICurrencyDataService dataService, IEventAggregator eventAggregator)
		{
			EventAggregator = eventAggregator;
			DataService = dataService;
			
			DataService.GetCoins();
			/*CurrencyItems = new PagedCollectionView(DataService.Coins);*/
			/*CurrencyItems.CollectionChanged += CurrencyItemsCollectionChanged;*/
		}

		void CurrencyItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			IGL_coin coin = this.CurrencyItems.CurrentItem as IGL_coin;
			if (coin != null)
			{
				// Publish the CoinSelectedEvent event.
				EventAggregator.GetEvent<CoinSelectedEvent>().Publish(coin.coin_id.ToString());
			}
		}

		public string Title
		{
			get { return AVTRes.PageCurrency; }
		}
	}

The page can display the content UNLESS I remove the commented lines, which assign the CurrencyItems the PagedCollectionView value ... If I do so it doesn't show up ...

Another thing, If I change the CurrencyItems to type of IEnumeruable<tablename> it works ... BUT I need the PagedCollectionView to track the selection changes as the example..

Strange right !! what could be the reason for that ...

Any help appreciated

Waleed

 

Feb 7, 2011 at 2:36 PM

Hi Waleed,

Based on my understanding of your scenario, if you need to track the current item on the collection, you need to handle the CurrentChanged Event as the UIComposition QuickStart shows.

If you still need to handle the collectionChanged Event because a specific requirement, you could try refresh the items. For example:

[ImportingConstructor]
public CurrencyViewModel(ICurrencyDataService dataService, IEventAggregator eventAggregator)
{
      EventAggregator = eventAggregator;
      
      DataService = dataService;
      
      DataService.GetCoins();

      CurrencyItems = new PagedCollectionView(DataService.Coins);                    
      CurrencyItems.CollectionChanged += CurrencyItemsCollectionChanged;
      CurrencyItems.Refresh(); 
}

 

Please, let me know if this information helps you.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Feb 7, 2011 at 6:25 PM

I have used the Telerik VirtualQueryableCollectionVie and it works fine.

Best