Loose Xaml with Unity/Mef

Topics: Prism v4 - WPF 4
Dec 14, 2010 at 4:38 PM
Edited Dec 14, 2010 at 4:39 PM

I am looking for an example on the proper way to utilize a loose xaml2009 file that I will be reading from a database record or file and having it assigned into a region using MEF or Unity.  Every example I see deals with Xaml declared in the application, often with a code behind entry.  I need to see this done in VB.

Thanks,

Brian

Developer
Dec 14, 2010 at 7:32 PM
Edited Dec 14, 2010 at 8:01 PM

Hi Brian,

You might find the following threads useful to your purpose, as well as the XamlReader class article in MSDN:

Also, as this is not strictly related to Prism, you might find better support in the WPF forums.

Once you've obtained a reference to the control defined in your XAML, you can register it in the corresponding container. The procedures to do so can be found in this chapter from the Prism MSDN documentation, under the "Registering" section.

When you have registered your control in the corresponding container, you can show it in a Region using any of the mechanisms for UI Composition, following the approaches explained in this chapter from the Prism MSDN documentation.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Dec 14, 2010 at 8:00 PM

Guido,

Thanks.

I have already worked a test case in plain WPF getting the xaml and placing it into a container on another view that uses a viewmodel,

so I am familiar with the XamlReader class and applying it into another xaml object.

I am new at .Net and I am trying to understand some things. Lambda expressions I have not fully understood yet.

We want to use Prism and either MEF or Unity, whichever is better for our concepts.

We do plan on showing multiple instances of the same views both as separate tasks and when exposing details of transactions that may be in a list or datagrid.

We plan to use a lot of dynamic xaml, as we display/hide fields/options based on user defined security rights, so we don’t have a fixed view.

We also have a variety of clients with different needs and even different labels for the same in internal item, so allowing them to have the xaml in a file or db allows us to easily adapt it for the site.

I am planning to use comments in the xaml to remove sections based on the security options from the file before having it processed to deal with individual user/group rights.

This will allow us to make changes only in the xaml to support relabeling or restructuring the view that each site/user sees.

What I am trying to find is just what would I need to do assuming I have a usercontrol defined in xaml that is in a filestream object.

I am still at concept stage with our app refractor, just trying to get some examples running to show that the ideas will work, that was why I was hoping to find a real example I could follow.

Once you've obtained a reference to the control defined in your XAML, you can register it in the corresponding container. The procedures to do so can be found in this chapter from the Prism MSDN documentation, under the "Registering" section.

Assuming I have a filestream object that has a usercontrol in it, what would I need to do to properly register it in VB?

Do I need to go through the resolving step?

Your last link did not go to MSDN documentation, so I am not sure what you were trying to show me.

Brian



This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error, please notify the system manager. This message contains confidential information and is intended only for the individual named. If you are not the named addressee, you should not disseminate, distribute, or copy this email. Please notify the sender immediately by email if you have received this email by mistake and delete this email from your system. If you are not the intended recipient, you are notified that discolosing, copying, distributing, or taking any action in reliance on the contents of this information is strictly prohibitied.
Dec 15, 2010 at 8:21 PM

Hi Brian,

As this is not specifically related to Prism, you might find these links useful:

On the other hand, based on my understanding of your scenario, you might find useful Prism´s UI composition documentation:

I hope you find this information handy.

Thanks,

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

Jan 19, 2011 at 4:51 PM

We have loose Xaml loading non prism and we have prism loading different views using unity containers.  What I am looking for is how to properly read a XAML usercontrol loose file and have it injected into the containers. 

We currently get the xaml into a dependency object and locate a content container on our window and assign it to that.  Since we are not dealing with a window now, just the view, what do we need to do.

Currently we have the following executed:

Me.regionManager.RegisterViewWithRegion(RegionNames.LeftRegion, _
                                                    Function() Me.container.Resolve(Of ModuleControl)())
I want to replace the modulecontrol view with a loose xaml file we have called MC_loose which has a usercontrol defined in it.
I am trying some things out for general proof of concept, I really need the VB code to get the xaml in and tied to the region.
We are loading the xaml for the window based method using the following:
 Dim rootElement As DependencyObject
            Using fs As New FileStream("MC_loose.xaml"FileMode.Open)
                rootElement = CType(XamlReader.Load(fs), DependencyObject)
            End Using

and then assigning it to a content control.content that is looked up on the window. 

Thanks for any code samples,
Brian


Developer
Jan 20, 2011 at 3:11 PM
Edited Jan 20, 2011 at 4:37 PM

Hi Brian,

Once you have obtained your MC_loose.xaml view through the use of the XamlReader, and have it set into a DependencyObject, you could register that instance in Unity, so that it can be retrieved from other modules.

The code for registering it into the Unity Container (using an overload of the RegisterInstance method) could be something like this:

 

Me.container.RegisterInstance(GetType(DependencyObject), "MC_loose", rootElement)

 

and the code for retrieving it and adding it to your region could look like this:

Me.regionManager.RegisterViewWithRegion(RegionNames.LeftRegion, _
                                                    Function() Me.container.Resolve(Of DependencyObject)("MC_loose"))

Let me know if we have misunderstood your scenario. If that is the case, it would be helpful if you could provide us with more information regardin your requirement.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jan 20, 2011 at 3:43 PM

You use ModuleControl, but for us, this is not resolved.  Are we missing something?

Resolve(Of ModuleControl)("MC_loose"))

(I replaced it with UserControl, it runs, but the control is not present.)

Brian

 

Developer
Jan 20, 2011 at 4:38 PM

Hi Brian,

I'm sorry, I meant to resolve a named instance of the type DependencyObject, not ModuleControl. I've updated my previous answer to reflect this.

Me.regionManager.RegisterViewWithRegion(RegionNames.LeftRegion, _
                                                    Function() Me.container.Resolve(Of DependencyObject)("MC_loose"))

Sorry for the inconveniences.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jan 20, 2011 at 4:45 PM

That's it!

Thanks.

What we didn't know was the RegisterInstance command and the Resolve(of DependencyObject).

We are now moving on!

Brian