0

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

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

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

谢谢!

4

1 回答 1

4

您可以在 a 中显示任何内容NavigationWindow,而不仅仅是Pages. 使其工作的一种简单方法是在NavigationWindow's 资源DataTemplate中为要显示的每个 ViewModel 定义 a 。Content将 的属性绑定NavigationWindow到主 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>

应用程序.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从资源中选择合适的。

于 2010-01-19T20:47:02.240 回答