Reg:在WPF App中基于棱镜查看导航 [英] Reg: View navigation in Wpf App base on prism

查看:157
本文介绍了Reg:在WPF App中基于棱镜查看导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用于学习目的的小应用程序.在我的外壳中,我使用了一个itemControl,它定义了一个区域,并且我已经向该区域注册了三个视图,在我的外壳中,我有另一个区域,并为其注册了一个视图,该视图包含3个按钮,我的要求是,当我单击button1,view1突出显示,其他2个视图被取消显示或禁用,以同样的方式单击第二个按钮时,view2应该被点亮.
我试图从4天开始实现这一目标,但是没有运气.

I created a small app for learning purpose . in my shell I took a itemsControl which defines a region and I have registered three views to this region,in my shell I have one another region and registed a view to it,the view contains 3 buttons,my requirement is that when I click on button1,view1 get highlighted and other 2 view either disapper or disable,same way when click second button,view2 should be liglllited.
I am trying to achive this since 4 days but no luck.
any help would be appericiated.

推荐答案

您首先要关注的是对RegionManager的引用.到目前为止,我知道两种获取shell的regionmanager的方法.当您在另一个视图或视图模型中时,这两种方法也都将生成单词.可能还有其他使用MEF或其他依赖注入系统的方法(例如Castle Windsor).

第一种方法假定您使用Unity并使用了UnityBootstrapper的实现.您可能在Prism目录(安装棱镜的位置)中的Prism4.chm文件中介绍了此方法.它在第2章(初始化Prism应用程序)中.

运行引导程序后,可以使用ServiceLocator来找到默认的RegionManager(外壳程序使用默认的RegionManager).

Your first concern is to get a reference to the RegionManager. So far I know of 2 ways to get the regionmanager of your shell. Both methods will also word when you are in another view or a viewmodel. There might be other methods using MEF or other Dependency Injection systems (like Castle Windsor).

The first method assumes that you use Unity and have used an implemenation of the UnityBootstrapper. This method is descibed in the Prism4.chm file which you probably have found in Prism directory (where you did install prism). It is in Chapter 2 (Initializing Prism applications).

Once you have run the bootstrapper you can use the ServiceLocator to locate the default RegionManager (the shell uses the default RegionManager).

// Import the Microsoft.Practices.Prism.Regions, Microsoft.Practices.ServiceLocation and (maybe) Microsoft.Practices.Unity

IRegionManager myRegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();



第二种方法可以在不使用依赖系统的情况下获得对RegionManager的引用.这使用RegionBehaviour的常规实现.这在 http://stackoverflow.com/questions/6582612/prism-4-locally- scoped-regionmanager [ ^ ].使用此答案中的代码,您可以使View和ViewModel知道它的RegionManager.


拥有regionmanager之后,您将需要对包含3个视图的区域的引用.



The second method makes it possible to get a reference to the RegionManager without using a Dependency System. This uses a cusom implementation of a RegionBehaviour. This is descibed in http://stackoverflow.com/questions/6582612/prism-4-locally-scoped-regionmanager[^]. Using the code in this answer you can make View and ViewModel aware of it''s RegionManager.


Once you have the regionmanager you will need a reference to the region containing the 3 views.

IRegion myRegion = myRegionManager.Regions["theNameOfYourRegion"];



要获取对视图的引用,请使用



To get a reference to the view, you use

//use namespace System.Linq

FrameworkElement aView = myRegion.Views.ElementAt(i) as FrameworkElement;




将所有内容放在一起,并假设您在后面使用代码来处理按钮的clickevent,则可能会得到以下内容:




Putting it all together and assuming that you use code behind to handle the clickevent of the buttons, you might get something like this:

private void Button1_Click(object sender, RoutedEventArgs e)
{
    ShowViewAtIndex(0);
}

private void Button2_Click(object sender, RoutedEventArgs e)
{
    ShowViewAtIndex(1);
}

private void Button3_Click(object sender, RoutedEventArgs e)
{
        ShowViewAtIndex(2);
}

private void ShowViewAtIndex(int index)
{
    IRegionManager myRegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
    IRegion myRegion = myRegionManager.Regions["theNameOfYourRegion"];

    int numberOfViews = myRegion.Views.Count();
    for (int i = 0; i < numberOfViews; i++)
    {
        FrameworkElement aView = myRegion.Views.ElementAt(i) as FrameworkElement;
        if (aView != null && i == index)
            aView.Visibility = System.Windows.Visibility.Visible;
        else
            aView.Visibility = System.Windows.Visibility.Collapsed;
    }
}



我希望这会有用.



I hope this is of any use.


这篇关于Reg:在WPF App中基于棱镜查看导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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