Skip to main content
 首页 » 编程设计

c#之在 MVVM WPF 应用程序中处理导航

2024年02月13日10zengkefu

我正在开发遵循 MVVM 的 WPF 应用程序。现在我正在按以下方式处理 View 导航。 主窗口 View

<Border> 
    <StackPanel> 
    <local:Home 
               Content="{Binding CurrentView,Converter={StaticResource ViewConverterHome}, UpdateSourceTrigger=PropertyChanged}"/> 
    <local:Page1 
            Content="{Binding CurrentView,Converter={StaticResource ViewConverterPage1}, UpdateSourceTrigger=PropertyChanged}"/> 
    <local:Page2 
        Content="{Binding CurrentView,Converter={StaticResource ViewConverterPage2}, UpdateSourceTrigger=PropertyChanged}"/> 
    </StackPanel> 
 
</Border> 

主页、第 1 页、第 2 页是 3 个 View 。 HomeVM、Page1VM、Page2VM是 View 对应的 View 模型。有一个名为 ApplicationViewModel 的类,它包含 CViewModelBase 类型的 CurrentView 属性,它是所有三个 View 模型的父类。 ApplicationViewModel 按以下方式处理导航

   private void OnUserInputNextClicked(object sender, OperationInformationChangedEventArgs e) 
    { 
        do 
        { 
            if (this.CurrentView is HomeVM) 
            { 
                this.CurrentView = null; 
                Page1VM page1 = new Page1VM("BNM", "MATH HONS", "13"); 
                page1.NextCilcked += new EventHandler<OperationInformationChangedEventArgs>(OnUserInputNextClicked); 
                page1.BackCilcked += new EventHandler<OperationInformationChangedEventArgs>(OnUserInputBackClicked); 
                this.CurrentView = page1; 
                break; 
            } 
 
            if (this.CurrentView is Page1VM) 
            { 
                this.CurrentView = null; 
                Page2VM page2 = new Page2VM("Kolkata", "Monoj", "Itachuna"); 
                page2.NextCilcked += new EventHandler<OperationInformationChangedEventArgs>(OnUserInputNextClicked); 
                page2.BackCilcked += new EventHandler<OperationInformationChangedEventArgs>(OnUserInputBackClicked); 
                this.CurrentView = page2; 
                break; 
            } 
            if (this.CurrentView is Page2VM) 
            { 
                this.CurrentView = null; 
                HomeVM home = new HomeVM("Anirban", "30"); 
                home.NextCilcked += new EventHandler<OperationInformationChangedEventArgs>(OnUserInputNextClicked); 
                this.CurrentView = home; 
                break; 
            } 
        } while (false); 
    } 

导航工作正常;但是处理消失的 View 并没有被调用。所以所有的 View 都存在到最后。有什么办法可以防止这种情况发生吗?

请您参考如下方法:

您的 View 将始终存在,因为您使用 XAML 将每个 View 的副本添加到您的 UI,即使其中包含的 Content 可能不存在也是如此

通常我会使用 ContentControl 来显示内容,而不是为每个内容类型创建一个控件实例,并且我会使用 DataTemplates 告诉 WPF 如何绘制每种类型的内容。

例如,

<Window.Resources> 
    <DataTemplate DataType="{x:Type local:HomeVM}"> 
        <local:Home Content="{Binding }" /> 
    </DataTemplate> 
    <DataTemplate DataType="{x:Type local:Page1VM}"> 
        <local:Page1 Content="{Binding }" /> 
    </DataTemplate> 
    <DataTemplate DataType="{x:Type local:Page2VM}"> 
        <local:Page2 Content="{Binding }" /> 
    </DataTemplate> 
</Window.Resources> 
 
<Border> 
    <StackPanel> 
        <ContentControl Content="{Binding CurrentView}" /> 
    </StackPanel> 
</Border> 

这样,您在 VisualTree 中只有一个 Content 实例,DataTemplate WPF 用户根据它的 DataType< 绘制您的内容更改.

我有一个 example of this kind of navigation with WPF如果您有兴趣查看完整的代码示例,请访问我的博客