我目前正在编写一个应用程序,复合方法就像手套一样适合......几乎!
我还需要一种在视图之间导航的方法,包括维护用于向后和向前导航的日志。
结合这两种方法的最佳方式是什么,一方面是基于单一Window
的 CAG shell 及其UserControl
派生视图,另一方面是方便的NavigationWindow
shell 及其Page
派生视图和日志?
谢谢!
我目前正在编写一个应用程序,复合方法就像手套一样适合......几乎!
我还需要一种在视图之间导航的方法,包括维护用于向后和向前导航的日志。
结合这两种方法的最佳方式是什么,一方面是基于单一Window
的 CAG shell 及其UserControl
派生视图,另一方面是方便的NavigationWindow
shell 及其Page
派生视图和日志?
谢谢!
您可以在 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
从资源中选择合适的。