视图未在区域中显示

发布于 2024-11-28 05:28:28 字数 697 浏览 0 评论 0原文

对于以下场景,我有两个问题:为什么该区域最初没有添加到区域集合中?

为什么该区域没有显示视图...或者如何排除故障?

问题是:在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-12-05 05:28:28

第一:

你在哪里将视图和视图模型添加到 Unity 中?您应该将其作为模块初始化的一部分。每个 Prism 模块都应该有一个实现 IModule 的类。

假设您已完成此操作:

阅读上面的代码,我看到正在创建一个区域,但您尚未添加要绑定到该区域的控件的 Xaml。

例如,我希望在 Shell 中看到以下内容:

<UserControl>
<Grid>
<ItemsControl Margin="0,20,0,0" cal:RegionManager.RegionName="MainWindowRegion" />
</Grid>
</UserControl>

这将创建 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:

<UserControl>
<Grid>
<ItemsControl Margin="0,20,0,0" cal:RegionManager.RegionName="MainWindowRegion" />
</Grid>
</UserControl>

This would create the MainWindowRegion and bind it to the ItemsControl on the page. When this user control was loaded it's region would be registered with Prism and added to the default RegionManager.

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 a SomeViewModel parameter in its constructor and then, bind it to DataContext there. Unity will see that SomeView has a dependency and automatically resolve it...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文