Quantcast
Viewing all articles
Browse latest Browse all 18858

MEF: Possible to register the same ViewModel with Different names?

Hi,

Can you create multiple NonShared ViewModels with a different name?  My problems is I have a composite view:

<UserControl x:Class="Natsar.ODS.Documents.Spoken.SpokenView"
.
..
...<Grid x:Name="LayoutContent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"><Border Padding="5,5,5,5" BorderBrush="#77000000" BorderThickness="1,1,1,1" Margin="0,0,0,5" CornerRadius="12,12,12,12" Width="Auto"><ContentControl x:Name="FlowDocumentDetails" Content="{Binding FlowDocumentDetails}"><ContentControl.ContentTemplate><DataTemplate><flowdoc:FlowDocumentView/></DataTemplate></ContentControl.ContentTemplate></ContentControl></Border></Grid></UserControl>
 

..the SpokenView gets created and added manually when the user clicks aButton on the CustomTabControl.  TheButton click event is added to the TabControl during initialization of the template:

Image may be NSFW.
Clik here to view.

public override void OnApplyTemplate()
{
	base.OnApplyTemplate();

	.
	..
	...
	
	// set up the event handler for the 'New Tab' Button Click event
	_addNewButton = this.Template.FindName("PART_NewTabButton", this) as ButtonBase;
	if (_addNewButton != null)
		_addNewButton.Command = this.VerseTabCommand;
}

..and here is when the SpokenView is manually created. During this initialization theflowdoc:FlowDocumentView created automatically:

public void AddTabItem(SelectedReferenceState selectedReferenceState, /*SpokenTabType spokenTabType, */string parameter)
{
	.
	..
	...
		SpokenTabItem tabItem;

		// Using Items Property
		ISpokenViewModel spokenViewModel = ServiceLocator.Current.GetInstance<ISpokenViewModel>();
		SpokenView view = new SpokenView(selectedReferenceState);
		view.ViewModel = spokenViewModel;
		spokenViewModel.SelectedReferenceState = selectedReferenceState;
		tabItem = new SpokenTabItem { Header = parameter, Content = view, SpokenTabKey = parameter };

		int v = 0;
		if (selectedReferenceState.spokenTabType == SpokenTabType.Verse)
			v = this.Items.Add(tabItem);
		else
			if (selectedReferenceState.spokenTabType == SpokenTabType.Chapter)
				this.Items.Insert(0, tabItem);
			else
				if (i == -1 || i == this.Items.Count - 1 || AddNewTabToEnd)
					v = this.Items.Add(tabItem);
				else
				{
					v = i;
					this.Items.Insert(++i, tabItem);
				}
	...
	..
	.
}

..but, I'm unable to get the FlowDocumentViewModel of the FlowDocumentView from the SpokenViewModel when its being automatically created when theSpokenView has completed its initialization.  

I hope I can get you to understand this.  Each TabItem needs its ownNonShared objects of SpokenView andFlowDocumentViewModel, but at the same time SpokenView needs access to theFlowDocumentViewModel.

So is there a way I can create an object instance with a unique name likeUnity.  Is this available in MEF OR I how can accomplish this?  This looked interesting C# MEF: Exporting multiple objects of one type, and Importing specific, but still trying to visually construct this to work for my situation.

Each TabItem has to be unique because each TabItem will have different content which could go well over 1,000 Tabs.


Code is like a box of chocolates!...








Viewing all articles
Browse latest Browse all 18858

Trending Articles