一个视图模型和多视图 [英] One ViewModel and multiple views

查看:133
本文介绍了一个视图模型和多视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于Windows Phone 8的,以及MVVM模式的一些问题。

I've got some questions about Windows Phone 8, and the MVVM pattern.


  1. 我不知道如何我可以绑定许多显示的页面元素给一个视图模型(只有一个视图模型,因为我想用Facade模式)。

  1. I'm wondering how I can bind elements from many displayed pages to one ViewModel (there is only one ViewModel, because I want to use the Facade Pattern).

每个教程中,我看到了包括代码中的视图模型和模型中的静态字段。我不知道这是否正确。谁能告诉我在哪里,在WP8应用程序中的新的模式和视图模型应该建立这样做的权利? (所谓正确我的意思是也,我可以绑定多个页面中的元素,这个视图模型)。我正在考虑App.xaml.cs文件,但仍不能确定。

Every tutorial I saw includes code where the ViewModel and Model are in static fields. I'm not sure about correctness of this. Can someone tell me where in a WP8 app the new Model and ViewModel should be created to do it right? (By "right" I mean also that I can bind elements from multiple pages to this one ViewModel.) I was considering the App.xaml.cs file but still not sure.

感谢您的帮助!

推荐答案

最近我一直在困扰我自己有类似的问题。最后,我用 App.xaml.cs 以创建视图模型。

I've been recently bothering myself with similar questions. In the end I used App.xaml.cs to create the viewmodel.

是,它是建立在App.xaml.cs静态视图模型正确的方式,因为应用类是入店从应用程序的任何页面,这也是正确的声明它们静态的,因为你会想,而无需创建应用程序的实例来访问它,正如塔里克在他的回答中写道:

Yes, it is a correct way to create a static viewmodel in App.xaml.cs, because the App class is accesible from any page in the application, it is also correct to declare them static, because you're going to want to access it without creating the instance of App, and And as Tariq wrote in his answer:

视图模型和模型静态字段,所以如果他们走出去的范围值不被破坏。这进一步支持从多个页面方便地升级

ViewModel and Model are static fields so the values are not destroyed if they go out of scope. This further enables easy updating from multiple pages.

编辑:只是要小心,当你的网页浏览之间,和导航回来,绑定一旦你在内存中回到页面不会自动恢复。

我已将此添加到 App.xaml.cs 旁边的定义 RootFrame

I added this to the App.xaml.cs next to the definition of RootFrame:

private static MainViewModel viewModel; //not sure how your viewmodel class is named
public static MainViewModel ViewModel   //and a property to access it from
{
  get
  {
    if(viewModel == null)               //which creates the viewModel just before
       viewModel = new MainViewModel(); //it's first used
    return viewModel;
  }
}



<击>当我想绑定的东西我的网页,JST已将此添加到页面的构造器( InitializeComponents()之后; ):

DataContext = App.ViewModel;



WHE你要绑定,你最好设定在绑定的的OnNavigatedTo()(只要不是太资源打造昂贵的 - 如果它要采取比一些时间或资源更多,你。应该考虑重新加工您的视图模型,以便它加载一段时间内)

Whe you want to bind, you better set the binding in an OnNavigatedTo() (provided it's not too resource expensive to build - if it's going to take more than some time or resources, you should consider reworking your ViewModel so it loads over time).

只需添加到您的网页:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);             //not needed, base method is empty and does nothing
        DataContext = null;                //important part, whenever you navigate, refreshes the ViewModel - no deletion, just resetting of the DataContext, so the page won't get stuck
        DataContext = App.ViewModel;      //and finally the resetting
    }



为了解释这个代码做什么和为什么我修改这样我的代码:


  • 在当前让我的应用程序,我一直就用自己在应用程序导航像backbrowsing - 当我按下硬件后退按钮,并得到了以前的网站,该值分别为持平,即使我改变了他们在我从backbrowsed页面。他们在模型好,但没有正确绑定时,backbrowsing。

当我试着去解决它,它来到。我到底

As i tried to solve it, it came to me in the end.

所有你需要做的刷新绑定是:

All you need to do to refresh the binding is:


  • 再设置它,因为backbrowsing当页面的构造器不叫,唯一的地方刷新这是的OnNavigatedTo()事件。

我测试了它和它的作品

(因此,在创建时的第一页,并试图将数据绑定,视图模型是autmatically只是一个GET请求:)创建)

和在XAML中,我可以仅仅通过绑定:; TextBlock的文本={结合SomePropertyNameFromViewModel}/>

And in xaml, i can just bind by:

<TextBlock text="{Binding SomePropertyNameFromViewModel}" />

<TextBlock text="{Binding SomeModelInViewModel.ItsProperty}" />

或例如:

<ListBox IemSource="{Binding SomeCollectionInViewModel}">
...rest omitted for brevity...



等等...

et cetera...

和最好的事情?它的工作原理,它是相当容易的。我使用它的ViewModel与几个命令和包含绑定,属性和集合约6或7模型越来越填充为用户穿过应用程序,并指定了加载。

And the best thing? It works and it's fairly easy. I'm using it for ViewModel with several commands and about 6 or 7 models containing properties and collections for binding, that are getting populated as user goes through the app and specifies what to load.

PS:从我得到这个是要做到这一点,甚至连基本的WP枢应用程序做它像这样

您可以检查自己,如果你创建一个空的支点应用程序的外观到 App.xaml.cs ,也将是只创建这样的视图模型。而且它是从每一页入店。

You can check yourself if you create an empty pivot app an look into App.xaml.cs, there is going to be a viewmodel created just like this. And it's accesible from every page.

这篇关于一个视图模型和多视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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