tags:

views:

824

answers:

2

Hello, I have a small problem with Regions in PRISM. All basics tests work fine, but now I want to replace the following XAML with pure C#:

<UserControl x:Class="CAL.Modules.Simple.Region_Testing.RegionManagerTypes.XAML.ItemsControlRegionAdapterTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cal="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation"
    Height="Auto" Width="Auto">
        <ItemsControl cal:RegionManager.RegionName="ItemsControlRegionAdapterTestRegion"/>
</UserControl>

The code inside my testing class is fairly simple, I access the RegionManager and add some test views. However, as you see in the XAML above, there is actual nothing happening in the UserControl except from attaching the RegionManager to the Control. I am sure this must be possible in Code, extending the following lines I already have:

        // MISSING
        // Creating the UserControl in CODE instead of XAML


        // Create the UserControl and add it to the main window
        regionManager.AddToRegion(RegionNames.MainRegion, new ItemsControlRegionAdapterTest());

        // Add some views to the region inside the user control
        var currentTestRegionName = TestingRegionNames.ItemsControlRegionAdapterTestRegion;
        regionManager.Regions[currentTestRegionName].Add(new BlueView());
        regionManager.Regions[currentTestRegionName].Add(new RedView());

Thanks for any tips...

A: 

Try the XamlReader approach:

private const string xaml = @"
<UserControl x:Class=""CAL.Modules.Simple.Region_Testing.RegionManagerTypes.XAML.ItemsControlRegionAdapterTest""
    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
    xmlns:cal=""clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation""
    Height=""Auto"" Width=""Auto"">
        <ItemsControl cal:RegionManager.RegionName=""ItemsControlRegionAdapterTestRegion""/>
</UserControl>";

//in some method
public void RunMethod()
{
     //create object and cast it
     ItemsControlRegionAdapterTest obj = (ItemsControlRegionAdapterTest) XamlReader.Parse(xaml);
}
kek444
Yes, it is working, and nicer than a XAML File (cleaner). But looks still a little bit ugly. Added "working" example in a Reply (if anybody needs it). Perhaps somebody has another approach, but thanks. At least no XAML file anymore...
Christian
A: 

Ok, the XamlReader Approach is working (little corrections, see source code attached)...

But honestly, it looks a little bit ugly :-) So if anybody knows how to "attach the regionManager in Code", details are welcome. First, the working XAML reader lines are:

        // MISSING
        // Creating the UserControl in CODE instead of XAML
        var obj = (UserControl)XamlReader.Parse(@"<UserControl xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                                                               xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:cal=""clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation""
Height=""Auto"" Width=""Auto"">
    <ItemsControl cal:RegionManager.RegionName=""ItemsControlRegionAdapterTestRegion""/></UserControl>");

        // Create the UserControl and add it to the main window
        regionManager.AddToRegion(RegionNames.MainRegion, obj);

GOT IT!! (at least it is working, not sure if best practice)

        var uC = new UserControl();
        var iC = new ItemsControl();
        uC.Content = iC;

        RegionManager.SetRegionName(iC, "ItemsControlRegionAdapterTestRegion");

        regionManager.AddToRegion(RegionNames.MainRegion, uC);

Thanks for all help... Comments still welcome...

Christian
Note that this "ugly" way is actually the way recommended by Microsoft. The code-only way is even uglier, and if you wish I may post that, too.
kek444
Do you mean it is recommended with more complex examples to use XAML reader. Because in this (very very simple) thing, I like the code approach better.. (was not that hard, just had to look at it a few times with some creative input from your side :-)
Christian
This is the correct way to do this. Note that it doesn't matter the order that you execute AddToRegion and SetRegionName. Just a tip.
Anderson Imes