在 Wpf 应用程序中使用 Prism 进行导航

发布于 2024-08-04 03:32:11 字数 361 浏览 0 评论 0原文

我正在 Wpf 中启动一个新项目,现在正在考虑使用 Prism。现在我只是尝试使用 Prism 设置应用程序的导航。不幸的是,我缺乏该框架的经验,使得入门有点困难。

更准确地说,我的第一个挑战是我有一个带有“导航/菜单”区域和“主”区域的应用程序。对于所有不同的主区域视图,导航区域都是相同的,因此我在 shell.xaml 中定义菜单。单击任何菜单项时,我想使用 Prism 添加该区域的视图。第一的;这是人们通常会使用 Prism 做的事情吗?如果是这样;典型的方法是什么?我的意思是在更结构化的层面上。

我的印象是 Prism 最终将使我的应用程序更具可扩展性,并且我发现我从中获得了一些其他优势 - 例如 IoC 容器。所以我想使用它 - 如果我只能完成第一步的话..

I'm starting a new project in Wpf, and am now looking into using Prism. For now I'm simply trying to set up the navigation of the application using Prism. Unfortunately my lack of experience with the framework makes it a bit difficult to get started..

To be more precise about my first challenge I have an application with a "navigation/menu" region and a "main" region. The navigation region will be the same for all different main region views, and I therefore define the menu in the shell.xaml. When clicking any menu item I'd like to add a view to the region using Prism. First; is this something one typically will use Prism for? If so; what's the typical approach? And I mean on a more structural level..

My impression is that Prism will make my application much more scalable in the end, and I see that I get some other advantages from it - like the IoC container. So I would like to use it - if I could only get through the first steps..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

烟沫凡尘 2024-08-11 03:32:11

我有一个示例,它使用了更多一点 CAG 风格的模块来提供菜单以及如何向区域添加视图。它应该让事情变得更清楚一些。

http://dl.getdropbox.com/u/376992/CAGMenus.zip

希望这有帮助,
安德森

I've got a sample that uses a little more of the CAG feel for modules contributing to a menu and how to add views to a region. It ought to make things a little more clear.

http://dl.getdropbox.com/u/376992/CAGMenus.zip

Hope this helps,
Anderson

秉烛思 2024-08-11 03:32:11

Bumbuska、

Prism 将是您实现此功能的好方法,一旦您了解了原理,就很容易做到。

我的方法是在主区域的启动事件中添加事件侦听器。当您在菜单中选择项目时,就会触发该事件。发生这种情况时,您的主区域将处理该事件,并且您从主区域清除当前视图。然后创建您要使用的新视图并添加它。

您的主要区域应该如下所示:

public void Initialize()
{
    Events.PageEvents.ClickedEvent1 ce1 = this.eventAggregator.GetEvent<Events.PageEvents.ClickedEvent1>();
    ce1.Subscribe(LoadView, ThreadOption.UIThread, true);
}

private void LoadView(Events.HomePageEvents.Clicked clicked1)
{
    IRegion mainRegion = RegionManager.Regions["MainRegion"];
    foreach (object view in new List<object>mainRegion.Views))
    {
        RegionManager.Regions["MainRegion"].Remove(view);
    }

    IModule firstModule = Container.Resolve<Modules.FirstModule>();
    firstModuleModules.Initialize();

}

我希望这能为您指明正确的方向。如果您需要更多信息,请告诉我。

Bumbuska,

Prism will be a great way for you to achieve this functionality and it is pretty easy to do once you understand the principals.

The way I will do it is to add event listeners in the start up event of your Main Region. When you select your item in the menu, you fire the event. When that happens your Main Region will handle the event and you clear the current view from the Main Region. Then create the new view you want to use and add it.

Your main region should look something like this:

public void Initialize()
{
    Events.PageEvents.ClickedEvent1 ce1 = this.eventAggregator.GetEvent<Events.PageEvents.ClickedEvent1>();
    ce1.Subscribe(LoadView, ThreadOption.UIThread, true);
}

private void LoadView(Events.HomePageEvents.Clicked clicked1)
{
    IRegion mainRegion = RegionManager.Regions["MainRegion"];
    foreach (object view in new List<object>mainRegion.Views))
    {
        RegionManager.Regions["MainRegion"].Remove(view);
    }

    IModule firstModule = Container.Resolve<Modules.FirstModule>();
    firstModuleModules.Initialize();

}

I hope that points you in the right direction. Please let me know if you need any more info.

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