我在PRISM中遇到一个小问题。所有基础测试都可以正常工作,但现在我想用纯C#替换以下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中看到的那样,除了将RegionManager附加到Control之外,UserControl中实际上没有任何事情发生。我确信这在Code中是可行的,扩展了我已经拥有的以下几行:
// 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 :(得分: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);
感谢所有帮助......评论仍然欢迎......
答案 1 :(得分: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);
}