WPF MVVM导航意见 [英] WPF MVVM navigate views

查看:160
本文介绍了WPF MVVM导航意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个视图的WPF应用程序。我想从视图1 toswitch查看2,并从那里我可以切换到多个视图。所以我想,在同一窗口中加载视图2​​在视图按钮。

I have a WPF application with multiple views. I want toswitch from view 1 to view 2 and from there i can switch to multiple views. So I want a button on view 1 that loads view2 in the same window.

我试过的那些东西,但不能让它的工作。

I tried those things, but cant get it to work.

  • MVVM Light toolkit (galasoft) for WPF - navigation through windows
  • http://blog.galasoft.ch/archive/2011/01/06/navigation-in-a-wp7-application-with-mvvm-light.aspx

从第一个环节的问题是,我不明白的viewmodellocator code。他们所谓的CreateMain();功能,但其中这个定义,我怎么可以切换到另一种观点从视图中。

From the first link the problem is that i dont understand the viewmodellocator code. They call the CreateMain(); function but where is this defined, and how can i switch to an other view from inside a view.

推荐答案

首先,你不需要任何的工具包/框架来实现MVVM。它可以是这样简单......让我们假设我们有一个 MainViewModel PersonViewModel CompanyViewModel ,每个都有自己的相关看法,每个延伸摘要基类 BaseViewModel

Firstly, you don't need any of those toolkits/frameworks to implement MVVM. It can be as simple as this... let's assume that we have a MainViewModel, and PersonViewModel and a CompanyViewModel, each with their own related view and each extending an abstract base class BaseViewModel.

BaseViewModel ,我们可以添加常用的性能和/或的ICommand 实例和实施 INotifyPropertyChanged的接口。因为它们都延长 BaseViewModel 类,我们可以有,可以设置为任何的 MainViewModel 类此属性我们的视图模型:

In BaseViewModel, we can add common properties and/or ICommand instances and implement the INotifyPropertyChanged interface. As they all extend the BaseViewModel class, we can have this property in the MainViewModel class that can be set to any of our view models:

public BaseViewModel ViewModel { get; set; }

当然,你会在正确实施 INotifyPropertyChanged的接口的的不像这个简单的例子属性。现在的App.xaml ,我们声明一些简单的的DataTemplate s到连接的视图模型的看法:

Of course, you'd be implementing the INotifyPropertyChanged interface correctly on your properties unlike this quick example. Now in App.xaml, we declare some simple DataTemplates to connect the views with the view models:

<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>

现在,无论我们用 BaseViewModel 实例之一,我们的应用程序,这些的DataTemplate 旨意告诉框架显示相关的视图,而不是。我们可以像这样显示出来:

Now, wherever we use one of our BaseViewModel instances in our application, these DataTemplates will tell the framework to display the related view instead. We can display them like this:

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

因此​​,所有我们现在需要做的切换到一个新的观点是设置从视图模型属性 MainViewModel 类:

ViewModel = new PersonViewModel();

最后,我们怎么改从其他观点有何看法?那么有几种可能的方式做到这一点,但最简单的方法就是增加一个绑定从子视图直接到的ICommand MainViewModel 。我使用的 RelayComand 自定义版本,但你可以使用任何你喜欢的类型,我猜你会得到的图片:

Finally, how do we change the views from other views? Well there are several possible ways to do this, but the easiest way is to add a Binding from the child view directly to an ICommand in the MainViewModel. I use a custom version of the RelayComand, but you can use any type you like and I'm guessing that you'll get the picture:

public ICommand DisplayPersonView
{
    get { return new ActionCommand(action => ViewModel = new PersonViewModel(), 
        canExecute => !IsViewModelOfType<Person>()); }
}

在子视图XAML:

<Button Command="{Binding DataContext.DisplayPersonView, RelativeSource=
    {RelativeSource AncestorType={x:Type MainViewModel}}, Mode=OneWay}" />

这就是它!享受。

That's it! Enjoy.

这篇关于WPF MVVM导航意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆