【问题标题】:How can I set two Content region in a WPF apps using Prism 6如何使用 Prism 6 在 WPF 应用程序中设置两个内容区域
【发布时间】: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 方法。请看我的回答。

标签: c# wpf xaml prism-6


【解决方案1】:

不要使用ViewModelLocator 创建MainWindowViewModel。在Bootstrapper 中自己创建它 MainWindow 和区域已创建:

protected override DependencyObject CreateShell()
{
    return Container.Resolve<MainWindow>();
}

protected override void InitializeShell()
{
    var mainWindowViewModel = Container.Resolve<MainWindowViewModel>();
    Application.Current.MainWindow.DataContext = mainWindowViewModel;
    Application.Current.MainWindow.Show();
}

protected override void ConfigureContainer()
{
    base.ConfigureContainer();
    Container.RegisterTypeForNavigation<TestUserControl>("firstUiDesign");
}

MainWindow.xaml 中删除它:

prism:ViewModelLocator.AutoWireViewModel="True">

【讨论】:

  • 这是一个不错的解决方案,我会尝试,但目前我已经通过以下方式解决了它。在 MainWindowViewModel 中,我在构造函数this._regionManager.RegisterViewWithRegion("ContentRegion1", typeof(firstUiDesign)); 中有以下代码,这样它在运行时显示了 FirstUiDesign。无论如何感谢您指出 Prism ViewModelLocator
猜你喜欢
  • 2014-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
  • 2021-04-12
  • 1970-01-01
  • 2011-01-16
相关资源
最近更新 更多