视图未在区域中显示
对于以下场景,我有两个问题:为什么该区域最初没有添加到区域集合中?
为什么该区域没有显示视图...或者如何排除故障?
问题是:在 Prism for WPF 中,我有一个充当弹出窗口的窗口。该窗口有一个使用区域属性的用户控件。由于某种原因,该窗口的区域没有出现在区域集合中。
为了将窗口的区域放入区域集合中,我通过代码完成了。在触发此窗口出现的模块中,我执行以下操作:
IRegion region = new SingleActiveRegion();
myRegionManager.Regions.Add("MainWindowRegion", region);
myContainer.Resolve<someViewModel>().Initialize();
SomeView someView = myContainer.Resolve<SomeView>();
SomeViewModel someViewModel = myContainer.Resolve<SomeViewModel>();
someView.DataContext = someViewModel;
myRegionManager.Regions["MainWindowRegion"].add(someView, "SomeView");
该区域现在与所有其他区域一起出现在区域集合中。但是, someView 永远不会显示在该区域中。
I have two questions about the scenario below: Why didn't the region originally get added to the region collection?
Why isn't the view displaying in the region...or how can I troubleshoot it?
Here is the issue: In Prism for WPF, I have a Window that acts like a popup. This window has a user control that uses the region attribute. For some reason, this window's region was not appearing in the region collection.
To get the window's region into the region collection, I did it through code. In the module that triggers the appearance of this window, I do:
IRegion region = new SingleActiveRegion();
myRegionManager.Regions.Add("MainWindowRegion", region);
myContainer.Resolve<someViewModel>().Initialize();
SomeView someView = myContainer.Resolve<SomeView>();
SomeViewModel someViewModel = myContainer.Resolve<SomeViewModel>();
someView.DataContext = someViewModel;
myRegionManager.Regions["MainWindowRegion"].add(someView, "SomeView");
The region now appears in the region collection, along with all of my other regions. However, someView never displays in the region.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一:
你在哪里将视图和视图模型添加到 Unity 中?您应该将其作为模块初始化的一部分。每个 Prism 模块都应该有一个实现
IModule
的类。假设您已完成此操作:
阅读上面的代码,我看到正在创建一个区域,但您尚未添加要绑定到该区域的控件的 Xaml。
例如,我希望在 Shell 中看到以下内容:
这将创建
MainWindowRegion
并将其绑定到页面上的ItemsControl
。当加载此用户控件时,其区域将向 Prism 注册并添加到默认的RegionManager
中。如果您必须手动创建自己的区域,那么它可能未绑定到控件,因此当您开始向该区域添加视图时,它们不会显示,因为该区域未绑定到控件。
查看
view/viewmodel
分辨率,有一个更简单的方法:让
SomeView
在其构造函数中获取SomeViewModel
参数,然后将其绑定到DataContext
那里。 Unity 将发现SomeView
具有依赖关系并自动解决它...First:
Where are you adding your views and viewmodels to Unity? You should be doing this as part of your module initialization. Each of your Prism modules should have a single class that implements
IModule
.Assuming you've done this:
Reading the code, above, I see a region being created but you've not added the Xaml for the control that is going to be bound to this region.
for instance, I'd expect to see the following in the Shell:
This would create the
MainWindowRegion
and bind it to theItemsControl
on the page. When this user control was loaded it's region would be registered with Prism and added to the defaultRegionManager
.If you're having to create your own Region manually then it's probably not bound to a control so when you start adding views to the Region they're not going to be displayed because the Region isn't bound to a control.
Looking at the
view/viewmodel
resolutions, there is an easier way:Have
SomeView
take aSomeViewModel
parameter in its constructor and then, bind it toDataContext
there. Unity will see thatSomeView
has a dependency and automatically resolve it...