如何访问app shell中的区域? [英] How do I access the regions in the app shell?

查看:124
本文介绍了如何访问app shell中的区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将视图注入我的shell。文档状态

Quote:

RegionManager类提供对应用程序中Region对象的访问。

但是当我初始化RegionManager类并尝试在此实例上调用 Regions [MAIN_REGION] 时,它表示在管理器中找不到这样的区域。



我尝试了什么:



我也尝试过使用全新的Region实例(因为文档推断无法访问全局RegionManager或知道shell中区域的那个)。



 Region v = new Region(); //也尝试了ServiceLocator.Current.GetInstance< RegionManager>(); 

v.RequestNavigate(MAIN_REGION,新的Uri(Login,UriKind.Relative)); //登录是一个有效的代码隐藏,而main_region是我的shell中的prism区域





上面没有显示该区域,大概是因为它可以找到视图而不是区域。当我不打算一直只将该视图加载到我的main_region中时,我必须 RegisterViewWithRegion 吗?

解决方案

我自己不与地区合作,但你知道你可以找到一个PRISM样品

项目:

GitHub - PrismLibrary / Prism-Samples-Wpf:演示如何在WPF中使用各种Prism功能的示例 [ ^ ]



该项目包含至少2个地区特定样本(见下文) - 这有助于回答你的问题吗?



Prism-Samples-Wpf / 02-master at master·PrismLibrary / Prism-Samples-Wpf·GitHub [ ^ ]



Prism-Samples-Wpf / 03-CustomRegions at master·PrismLibrary / Prism-Samples-Wpf·GitHub [ ^ ]


为了获得工作中的区域经理来自其他模块的shell,我将由Prism传递给Shell的RegionManager参数,并将其分配给成员字段。

 public partial class Shell:Window 
{

public IRegionManager x;

public Shell(IRegionManager regionManager)
{
InitializeComponent();

x = regionManager;
}
}





接下来,在Bootstrapper或调用模块的任何地方,抓住该成员字段并将其传递给目标模块的构造函数(在初始化方法之前调用)。

 protected override void InitializeShell()
{
Application .Current.MainWindow =(Shell)Shell;

Application.Current.MainWindow.Show();

IRegionManager u = ServiceLocator.Current.GetInstance< Shell>()。x;

IModule firstScreen = new LoginModule.LoginModule(u);





最后,在目标模块的构造函数中,您可以根据需要使用。



 public void Initialize(){

IUnityContainer con = ServiceLocator.Current.GetInstance< ; IUnityContainer>();

con.RegisterType< Object,Login>(Login);

Region mainRegion =(Region)_regionMan.Regions [MainRegion]; //在这里使用

mainRegion.RequestNavigate(new Uri(Login,UriKind.Relative));
}
}





希望这可以帮助其他人解决同样的问题。


I'm trying to inject a view into my shell. The docs state

Quote:

The RegionManager class provides access to the Region objects within the application.

but when I initialize the RegionManager class and attempt to call Regions["MAIN_REGION"] on this instance, it says no such region was found in the manager.

What I have tried:

I've also tried using a fresh Region instance altogether (since the docs infer no way to either access a global RegionManager or the one with knowledge of the regions in the shell).

Region v = new Region(); // also tried ServiceLocator.Current.GetInstance<RegionManager>();

v.RequestNavigate("MAIN_REGION", new Uri("Login", UriKind.Relative)); // Login is a valid code-behind while main_region is a prism region in my shell



The above does not display the region, presumably because it can find the view but not the region. Must I RegisterViewWithRegion when I don't intend to load only that view into my main_region all the time?

解决方案

I do not work with regions myself but did you know that you can find a PRISM samples
project at:
GitHub - PrismLibrary/Prism-Samples-Wpf: Samples that demonstrate how to use various Prism features with WPF[^]

This project contains at least 2 Region specific samples (see below) - does this help answering your problem?

Prism-Samples-Wpf/02-Regions at master · PrismLibrary/Prism-Samples-Wpf · GitHub[^]

Prism-Samples-Wpf/03-CustomRegions at master · PrismLibrary/Prism-Samples-Wpf · GitHub[^]


In order to obtain the region manager at work in the shell from other modules, I took the RegionManager argument passed into Shell by Prism, and assigned it to a member field.

public partial class Shell : Window
    {

        public IRegionManager x;

        public Shell(IRegionManager regionManager)
        {
            InitializeComponent();

            x = regionManager;
        }
    }



Next, in the Bootstrapper or wherever the module would be invoked from, grab that member field and pass it to your target module's constructor (which would be called before its initialize method).

protected override void InitializeShell()
        {
            Application.Current.MainWindow = (Shell) Shell;

            Application.Current.MainWindow.Show();

            IRegionManager u = ServiceLocator.Current.GetInstance<Shell>().x;

            IModule firstScreen = new LoginModule.LoginModule(u);



Finally, in the target module's constructor, you can use as desired.

    public void Initialize () {

        IUnityContainer con = ServiceLocator.Current.GetInstance<IUnityContainer>();

        con.RegisterType<Object, Login>("Login");

        Region mainRegion = (Region) _regionMan.Regions["MainRegion"]; // use here

        mainRegion.RequestNavigate(new Uri("Login", UriKind.Relative));
    }
}



Hope this helps someone else with the same problem.


这篇关于如何访问app shell中的区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆