WPF:具有页面导航的复合应用程序

发布于 2024-08-18 09:32:05 字数 271 浏览 3 评论 0原文

我目前正在编写一个应用程序,复合方法就像手套一样适合......几乎!

我还需要一种在视图之间导航的方法,包括维护用于向后和向前导航的日志。

结合这两种方法的最佳方式是什么?一方面是基于单一 Window 的 CAG shell 及其 UserControl 派生视图,另一方面是方便的 NavigationWindow shell 及其 Page 派生视图和日志?

谢谢!

I am currently writing an application to which the composite methodology fits like a glove.... almost!

I also need a way to navigate between views, including maintaining a journal for navigation backward and forward.

What is the best way to combine these two methodologies, on one hand the single Window based CAG shell with its UserControl derived views, and on the other hand the convenient NavigationWindow shell with its Page derived views and journal?

Thanks!

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

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

发布评论

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

评论(1

南街女流氓 2024-08-25 09:32:05

您可以在 NavigationWindow 中显示任何内容,而不仅仅是 Pages。使其工作的一个简单方法是在 NavigationWindow 的资源中为要显示的每个 ViewModel 定义一个 DataTemplate。将 NavigationWindowContent 属性绑定到主 ViewModel 的属性,就完成了:更改该属性将更新 NavigationWindow 内容,并且将自动选择适当的 DataTemplate


更新

我刚刚查看了我使用 NavigationWindow 的项目的代码。实际上我错了,通过绑定 Content 不起作用(或者也许它有效,但这不是我所做的)。相反,我创建了一个 INavigationService 接口,由我的 App 类实现,该类通过调用 NavigationWindow.Navigate 方法来处理导航。这样,导航历史记录就由 NavigationWindow 维护。

的摘录

这是我的项目MainWindow.xaml:

<NavigationWindow x:Class="MyApp.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:vm="clr-namespace:MyApp.ViewModel"
                  xmlns:view="clr-namespace:MyApp.View"
                  Title="{Binding Content.DisplayName, RelativeSource={RelativeSource Self}, FallbackValue=The Title}"
                  Height="600" Width="800">
    <NavigationWindow.Resources>
        <DataTemplate DataType="{x:Type vm:HomeViewModel}">
            <view:HomeView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:CustomerViewModel}">
            <view:CustomerView />
        </DataTemplate>
    </NavigationWindow.Resources>
</NavigationWindow>

App.xaml.cs

    ...

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        LoadConfig();

        MyApp.MainWindow window = new MainWindow();
        INavigationService navigationService = this;
        HomeViewModel viewModel = new HomeViewModel(navigationService);
        this.MainWindow = window;
        window.Navigate(viewModel);
        window.Show();
    }

:当我需要导航到另一个视图时,我只需调用Navigate 方法以 ViewModel 作为参数,WPF 会自动从资源中选择适当的 DataTemplate

You can display anything in a NavigationWindow, not just Pages. A simple way to make it work is to define in the NavigationWindow's resources a DataTemplate for each ViewModel you want to display. Bind the Content property of the NavigationWindow to a property of your main ViewModel, and you're done : changing that property will update the NavigationWindow content, and the appropriate DataTemplate will be picked automatically


UPDATE

I just looked at the code of a project of mine where I used a NavigationWindow. Actually I was mistaken, it doesn't work by binding the Content (or maybe it works, but that's not what I did). Instead I created a INavigationService interface, implemented by my App class, which handles the navigation by calling the NavigationWindow.Navigate method. That way, the navigation history is maintained by the NavigationWindow.

Here's an extract from my project

MainWindow.xaml :

<NavigationWindow x:Class="MyApp.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:vm="clr-namespace:MyApp.ViewModel"
                  xmlns:view="clr-namespace:MyApp.View"
                  Title="{Binding Content.DisplayName, RelativeSource={RelativeSource Self}, FallbackValue=The Title}"
                  Height="600" Width="800">
    <NavigationWindow.Resources>
        <DataTemplate DataType="{x:Type vm:HomeViewModel}">
            <view:HomeView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:CustomerViewModel}">
            <view:CustomerView />
        </DataTemplate>
    </NavigationWindow.Resources>
</NavigationWindow>

App.xaml.cs :

    ...

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        LoadConfig();

        MyApp.MainWindow window = new MainWindow();
        INavigationService navigationService = this;
        HomeViewModel viewModel = new HomeViewModel(navigationService);
        this.MainWindow = window;
        window.Navigate(viewModel);
        window.Show();
    }

When I need to navigate to another view, I just call the Navigate method with the ViewModel as a parameter, and WPF automatically picks the appropriate DataTemplate from the resources.

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