如何在没有数据模板的情况下在 wpf mvvm 中导航用户控件视图?



我是wpf的新人。所以我决定为自己制作一些基本的 mvvm wpf 应用程序。我的应用程序包含两个块:导航块和内容块。导航块包含一些用于更改内容块视图的按钮。正如我在制作 MVVM 应用程序之前所说,所以我的观点是用户控件。 我用谷歌搜索了如何进行一些导航。所以在我的应用程序中,我做了这个: 在主xml中,我写了这样的:

<DataTemplate DataType="{x:Type ViewModels:MainViewModel}">
<Views:MainView />
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModels:PersonViewModel}">
<Views:PersonView />
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModels:CompanyViewModel}">
<Views:CompanyView />
</DataTemplate>

在我的主视图模型中,我放置了当前视图模型参数,该参数保存有关当前视图模型(和视图(的信息。

在主xaml中,我实际上编写了(我的内容块(:

<ContentControl Content="{Binding CurrentViewModel}" />

所以现在我可以切换视图;

我的主要问题:

  1. 如果我有很多视图,则很难在主xaml中编写所有新的数据模板。如果我有 50 次观看?怎么不总是写呢?
  2. 我需要使用主页进行导航吗?(在 catel 中,mmvm light 他们使用用户控件而不是页面(
  3. 在 catel 中,我使用 viewModeToViewConverter 进行了导航,并且在主 xaml 中没有数据模板 - 如何做到这一点?我真的不明白它是如何工作的。

谢谢你的回答!

就我而言,我使用一个主窗口和多个用户控件在屏幕之间导航。

在 MainWindow.xaml 有UserControl

...
<UserControl Name="UserControl_UserControl" HorizontalAlignment="Stretch"></UserControl>
...

在 MainWindows.xaml 中.cs

public MainWindow()
{
this.InitializeComponent();
Switcher.Main = this;
Switcher.Switch(new MainPageControl());
}
public void Navigate(UserControl nextPage)
{
UserControl_UserControl.Content = nextPage;
Title = "MyApp | " + nextPage.Tag;
}

枯萎类:

public static class Switcher
{
public static MainWindow Main;
private static UserControl ShowingPage;
private static Window PopupWindow;
public static void Switch(UserControl newPage)
{
ShowingPage = newPage;
Main.Navigate(newPage);
}
......
}

无论您在哪里使用 Swither.Swith(...(,主窗口都会发生变化。

最新更新