开发者

View not displaying in Region

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.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜