2009-08-02 35 views
0

我在PRISM的Regions中遇到了一個小問題。所有的基礎知識的測試工作正常,但現在我想用純C#替換下面的XAML:PRISM RegionManager - ItemsControl區域管理器的非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> 

我的測試類中的代碼是相當簡單的,我訪問RegionManager,並添加一些測試的看法。但是,正如您在上面的XAML中看到的那樣,UserControl中除了將RegionManager附加到Control之外實際沒有任何事情發生。我相信這必須在代碼中可能延長以下行我已經有了:

 // 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()); 

感謝您的任何提示...

回答

0

嘗試XamlReader方法:

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); 
} 
+0

是的,這是工作,並更好的比一個XAML文件(清潔)。但看起來還是有點難看。在回覆中添加了「工作」示例(如果有人需要)。也許有人有另一種方法,但謝謝。至少沒有XAML文件... – 2009-08-02 22:55:37

1

好,在XamlReader方法是工作(小改正,看到源代碼附後)......

但說實話,它看起來有點醜:-) 因此,如果有人知道如何「在代碼中附加regionManager」,歡迎提供詳細信息。 首先,工作的XAML讀取器行是:

 // 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 !! (至少是工作,不知道最好的做法)

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

     RegionManager.SetRegionName(iC, "ItemsControlRegionAdapterTestRegion"); 

     regionManager.AddToRegion(RegionNames.MainRegion, uC); 

感謝所有幫助...評論仍然歡迎...

+0

請注意,這種「醜陋的」方式實際上是Microsoft推薦的方式。只有代碼的方式更加醜陋,如果你願意,我也可以發佈。 – 2009-08-02 23:01:18