我正在使用带有Prism和MEF的Silverlight 5。
我试图在运行时通过读取XAML文件替换我的shell,从中创建一个UIElement并用新的UIElement替换旧shell的内容。我正在使用XamlReader.Load()
。
这是有效的,直到我尝试创建Prism区域。
我可以在其中创建一个只包含一个棱镜区域的新Shell,但是当我在新shell中有两个或更多区域时,我得到的只是浏览器中的空白屏幕,并且没有错误消息。
有没有办法调试这个?为什么会发生这种情况?
代码:
创建UIElement并替换shell(在Shell.xaml.cs中):
DependencyObject rootObject = XamlReader.Load(XAMLFileString) as DependencyObject;
UIElement customShell = rootObject as UIElement;
this.Content = customShell;
这有效(一个地区):
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:prism="http://www.codeplex.com/prism">
<StackPanel>
<ContentControl prism:RegionManager.RegionName="Region1"/>
</StackPanel>
</UserControl>
这也有效(两个常规内容控件):
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:prism="http://www.codeplex.com/prism">
<StackPanel>
<ContentControl Content="C1"/>
<ContentControl Content="C2"/>
</StackPanel>
</UserControl>
但是这给了我一个空白的屏幕(两个棱镜区域):
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:prism="http://www.codeplex.com/prism">
<StackPanel>
<ContentControl prism:RegionManager.RegionName="Region1"/>
<ContentControl prism:RegionManager.RegionName="Region2"/>
</StackPanel>
</UserControl>
答案 0 :(得分:0)
我弄清楚问题是什么:实际上有一个异常,但它在App.xaml.cs中默默处理:
Microsoft.Practices.Prism.Regions.Behaviors.RegionCreationException: An exception occurred while creating a region with name 'Region1'. The exception was: System.ArgumentException: Region with the given name is already registered:
引发了异常,因为我试图创建一个名称已存在于原始Shell中的区域。更改区域名称后,一切正常。
我仍然不知道为什么我的白色屏幕有两个区域但没有一个区域(两个区域都抛出相同的例外,所以一个区域也不应该有效)。
要将shell替换为具有相同区域名称的另一个shell,可以在替换shell之前取消注册这些区域:
_regionManager.Regions.Remove(regionname)