我已经看到一些使用Prism for WPF在一个窗口中更改用户控件的示例,它看起来像这样:
Bootstrapper.cs
protected override void ConfigureContainer()
{
base.ConfigureContainer();
Container.RegisterType(typeof(object), typeof(ViewA), "ViewA");
Container.RegisterType(typeof(object), typeof(ViewB), "ViewB");
}
MainWindowViewModel.cs
public class MainWindowViewModel : BindableBase
{
private readonly IRegionManager _regionManager;
public DelegateCommand<string> NavigateCommand;
public MainWindowViewModel(IRegionManager regionManager)
{
_regionManager = regionManager;
NavigateCommand = new DelegateCommand<string>(ExecuteNavigateCommand);
}
private void ExecuteNavigateCommand(string uri)
{
_regionManager.RequestNavigate("ContentRegion", uri);
}
}
MainWindow.xaml
<Button Grid.Row="1" Grid.Column="1" Command="{Binding NavigateCommand}" CommandParameter="ViewA" FontSize="16" Content="View A" Margin="4"/>
<Button Grid.Row="1" Grid.Column="2" Command="{Binding NavigateCommand}" CommandParameter="ViewB" FontSize="16" Content="View B" Margin="4"/>
您可以点击它,视图也会发生变化。但是当你启动它时,没有加载用户控件,只加载主窗口。我的问题是如何在应用程序启动时将用户控件加载到MainWindow?
答案 0 :(得分:0)
这里有不同的选择。首先,注册该区域的视图,以便自动发现和显示该区域。如果您之后不想导航到该视图,则该方法有效。
_regionManager.RegisterViewWithRegion("MyRegion", typeof(ViewA));
或者,您可以在应用程序完成启动时导航到视图。也就是说,从Bootstrapper.InitializeModules
结束。如果引导程序不知道该视图,或者您现在也想要执行其他操作,那么您还可以发布ModulesInitialized
之类的事件,并让定义视图的模块订阅该事件。
// in the assembly defining the interfaces shared between your modules
public class ModulesInitialized : PubSubEvent {}
// in the bootstrapper.InitializeModules
Container.Resolve<IEventAggregator>().GetEvent<ModulesInitialized>().Publish();
// in the module defining viewA
_eventAggregator.GetEvent<ModulesInitialized>().Subscribe( () => _regionManager.Requestnavigate( "MyRegion", "ViewA" ), true );