处理MVVM WPF应用程序中的导航

时间:2013-03-15 06:25:43

标签: c# wpf mvvm

我正在开发一个遵循MVVM的WPF应用程序。现在我按以下方式处理视图导航。 MainWindow 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>

Home,Page1,Page2是3次观看。 HomeVM,Page1VM,Page2VM是与视图对应的视图模型。有一个类调用ApplicationViewModel,它包含一个CViewModelBase类型的属性CurrentView,它是所有三个视图模型的父类。 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);
    }

导航工作正常;但处理失踪的观点并没有被调用。所有观点一直持续到最后。有什么方法可以阻止这种情况吗?

4 个答案:

答案 0 :(得分:4)

您的视图将始终存在,因为您使用XAML将每个视图的副本添加到您的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

答案 1 :(得分:0)

您需要更改MainWindow的DataContext。这取决于您的集成。当我创建MVVM应用程序时,我所做的是将MainWindow对象传递给每个视图构造函数。每当我必须移动到下一页(如下一个按钮)时,我将MainWindow对象DataContext更改为新视图。

像这样。

public PageOneViewModel
{

    private MainWindow _mainWindow;
    public PageOneViewModel(MainWindow mainWindow)
    {
         // Here I am saving MainWindow object.
         _mainWindow = mainWindow;
    }

    public OnNext()
    {
         // Here I am changing the view.
         MainWindow.DataContext = new PageTwoViewModel(_mainWindow);
    }
}

答案 2 :(得分:0)

你考虑过使用Frame吗?

<Frame Name="YourFrame" Navigated="OnNavigated"/>

然后你可以打电话

  

YourFrame.CanGoBack(),YourFrame.GoBack()

答案 3 :(得分:0)

这是一个link我对带有工作源代码的类似问题的回答。我使用的技术与Faisal的解决方案有点相似。

如果您需要使用侧边菜单演示导航的良好可下载示例解决方案,请查看herehere(simpler example)