我正在按照MVVM模式使用WPF开发Prism Sample应用程序,但是当它通过下面的代码传递时,在我的应用程序中存在一些问题:
var view = _container.Resolve<DummyView>();
IRegion region = _regionMansger.Regions["ViewInjectionMain_MainRegion"];
我已经尝试了其他stackoverplow帖子中提出的解决方案,但是它不起作用。
class ViewInjectionMainViewModel : BindableBase
{
IUnityContainer _container;
IRegionManager _regionMansger;
public ViewInjectionMainViewModel(IUnityContainer container, IRegionManager regionManager)
{
_container = container;
_regionMansger = regionManager;
var view = _container.Resolve<DummyView>();
IRegion region = _regionMansger.Regions["ViewInjectionMain_MainRegion"];
region.Add(view);
}
}
class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return this.Container.Resolve<MainWindow>();
}
protected override void InitializeShell()
{
base.InitializeShell();
var regionManager = this.Container.Resolve<IRegionManager>();
if (regionManager != null)
{
regionManager.RegisterViewWithRegion("MainRegion", typeof(FirstView));
}
Container.RegisterType<object, RegionBasic>("RegionBasic");
Container.RegisterType<object, RegionControlMain>("RegionControlMain");
Container.RegisterType<object, DummyView>("DummyView");
Container.RegisterType<object, ViewInjectionMain>("ViewInjectionMain");
Application.Current.MainWindow.Show();
}
protected override void ConfigureModuleCatalog()
{
var catalog = (ModuleCatalog)ModuleCatalog;
catalog.AddModule(typeof(ModuleA.ModuleAModule));
catalog.AddModule(typeof(ModuleB.ModuleBModule));
catalog.AddModule(typeof(DummyModule.DummyModuleClass));
}
}
谢谢。
答案 0 :(得分:3)
regionManager.RegisterViewWithRegion("MainRegion", typeof(FirstView));
regionManager.RegisterViewWithRegion("ViewInjectionMain_MainRegion", typeof(DummyView)); //add this line
并通过Dispatcher获取区域(也应加载ViewInjectionMain.xaml以加载ViewInjectionMain_MainRegion)
class ViewInjectionMainViewModel :BindableBase
{
IUnityContainer _container;
IRegionManager _regionMansger;
public ViewInjectionMainViewModel(IUnityContainer container, IRegionManager regionManager)
{
_container = container;
_regionMansger = regionManager;
System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
{
var view = _container.Resolve<DummyView>();
IRegion region = _regionMansger.Regions["ViewInjectionMain_MainRegion"];
region.Add(view);
}));
}
}
希望这对您有帮助
答案 1 :(得分:0)
我测试了以下建议。我做了一些按钮,并将其绑定到“ DelegateCommand”。有用。但是当我做一些功能时,“ IRegion region = _regionMansger.Regions [” ViewInjectionMain_MainRegion“];”它在构造函数中调用无效。
RegionManager包含视图的viewmodel构造函数结束后的区域。