Reputation: 330
I'm using PRISM4 MVVM Pattern, and the views are loaded successfully and displayed on the appropriate regions when the application starts. When the application starts, the ViewModels are initialized automatically when the view is loaded. However, if I try to inject a new view on a new tab (new region), the ViewModel of the new view is not initialized. Here's the code for view injection:
IRegion region = regionManager.Regions["RegionNameGoesHere"];
var pane = new Views.ABCView() {Tag = id};
regionManager.Regions["RegionNameGoesHere"].Add(pane);
The code above opens a new tab and load the new view, but it doesn't initialize the ViewModel. Each tab of the control is a new region (there's a RegionAdapter for the tab control).
Here's the code behind the view:
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Docking;
namespace Company.Application.Module.Assembly.Views
{
[Infrastructure.Behaviours.ViewExport("ABCView")]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class ABCView : RadPane
{
public ABCView()
{
this.InitializeComponent();
}
/// <summary>
/// Sets the ViewModel.
/// </summary>
/// <remarks>
/// This set-only property is annotated with the <see cref="ImportAttribute"/> so it is injected by MEF with
/// the appropriate view model.
/// </remarks>
[Import]
[SuppressMessage("Microsoft.Design", "CA1044:PropertiesShouldNotBeWriteOnly", Justification = "Needs to be a property to be composed by MEF")]
ABCViewModel ViewModel
{
set
{
this.Decorator.DataContext = value;
//this.DataContext = value;
}
}
}
}
And here's the ViewModel with a few properties and events. I'm missing something in the code above to initialize the ViewModel below. Any suggestion is greatly appreciated.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel.Composition;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml;
using System.Xml.XPath;
using System.Windows.Data;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
namespace Company.Application.Module.Assembly.Views
{
[Export(typeof(ABCViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ABCViewModel : NotificationObject
{
private readonly IRegionManager regionManager;
[ImportingConstructor]
public ABCViewModel(IRegionManager regionManager)
{
// Event Aggregator
//this.eventAggregator = eventAggregator;
// Region Manager
this.regionManager = regionManager;
}
#region P R O P E R T I E S
#region E V E N T S
}
}
Upvotes: 2
Views: 3070
Reputation: 16744
The problem is that you are creating the view yourself instead of having the CompositionContainer create it for you. The CompositionContainer doesn't know anything about objects you create yourself, so when you call new Views.ABCView()
, the imports aren't magically satisfied.
With raw MEF, you would call CompositionContainer.GetExports() to get a view from the container. There is probably some infrastructure in Prism that wraps around this call, but I don't know much about Prism so I don't know exactly what that would be.
Upvotes: 2