【发布时间】:2017-06-27 10:10:54
【问题描述】:
我有一个场景。我正在使用 Prism 6.0 编写一个 WPF 应用程序,我想首先弹出一个子窗口,该窗口将具有用于三种不同 UI 设计的三个按钮。类似这样。
根据选择,我将更新MainWindowViewModel 并关闭子窗口,并显示MainWindow。
直到这部分是好的。但问题出在这部分之后,三个不同的按钮指向了三种不同的UI设计。特别是 ContentRegion1 和 ContentRegion2。这两个地区是不同的。
我已经看到,如果我通过 Button 输入命令,那么这段代码就会成功运行。但如果我把它放在MainWindowViewModel 中,同样的情况不会发生。
public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
{
_regionManager = regionManager;
_eventAggregator = eventAggregator;
_regionManager.RequestNavigate("ContentRegion1", "firstUiDesign");
...
}
主窗口看起来像这样...
ContentRegion1 和 ContentRegion2 是 XAML 中这样设计的两个
<Border CornerRadius="15" Grid.Column="0">
<StackPanel>
<ContentControl prism:RegionManager.RegionName="ContentRegion1" />
</StackPanel>
</Border>
<Border CornerRadius="15" Grid.Column="1">
<StackPanel Grid.Column="1" Margin="2">
<ContentControl prism:RegionManager.RegionName="ContentRegion2" />
</StackPanel>
</Border>
但是我无法弄清楚我做错了什么,或者我需要在代码中添加什么额外的东西才能使其正常工作。
即使在 BootStrapper.cs 我也有这个代码
引导程序代码:
protected override DependencyObject CreateShell()
{
//return base.CreateShell();
return Container.Resolve<MainWindow>();
}
protected override void InitializeShell()
{
Application.Current.MainWindow.Show();
}
protected override void ConfigureContainer()
{
base.ConfigureContainer();
Container.RegisterTypeForNavigation<TestUserControl>("firstUiDesign");
}
任何人都可以在这方面提供帮助。
【问题讨论】:
-
所以你的问题是不能在ContentRegion1中显示firstUiDesign视图?你的引导程序是如何实现的?
-
我已经用 Bootstrapper 代码更新了我的源代码
-
你在哪里创建 MainWindowViewModel?您是否使用 ViewModelLocator 为您创建它?
-
我用的是Prism的默认样式
xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" -
那是你的问题。在创建区域之前,您不能调用 RequestNavigate 方法。请看我的回答。