XAML数据绑定单样MVVM对象 [英] XAML Databinding singleton-like MVVM object

查看:121
本文介绍了XAML数据绑定单样MVVM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个菜鸟,我有时会停留在简单的和/或愚蠢的认为,这是其中之一。

我得到的数据绑定的总体思路,我已经通过了一些在网络上的教程,并得到了Google搜索,通过很多很多的文字了几个小时,只让我稍微感到困惑。

I get the general idea of databinding, I've got through some of the tutorials on the net and googled for a few hours through lots and lots of text, which only made me slightly confused.

我工作的的Windows Phone 8的C#/ XAML .NET 4.5应用程序

使用提供给我一些方法我加载数据,我需要查看(在不同的组合有时)web服务我需要他们大多存储的应用程序正在运行的时间。

Using a webservice provided to me with a few methods I'm loading data that I need to view (sometimes in different combinations) and I need to store most of them for the time the app is running.


  • 有关为此我创建了一个视图模型+几款机型并构建他们这样的:

MainViewModel
--------------
|
+ several properties (Username, Password, etc...)
|
+ Commands (loadData1, loadData2, flush, ...  - implementations of ICommand)
|
+ ------ PersonalInfoModel
|        -----------------
|        + several properties (name, surname, phonenumber, etc...)
|
|             
+ ------ DataGroup1Model
|        ---------------
|        +several properties
|        +ObservableCollection<Item1> (roughly 0 - 50 items)
|        +ObservableCollection<Item2> (roughly 0 - 5 items)
|        +ObservableCollection<string> (roughly 0 - 5 items)
|        
|                  Item1                         Item2
|                  -----                         -----
|                  +several properties           +several other properties
|                  
|
+ ------ DataGroup2Model (similar to previous)
...et cetera...

视图模型不填充一次,因为它不能的),而是数据的用户经过装进去该应用程序,并指定他想要的东西要加载的数据(的这主要是基于一些时间跨度和/或其他标准的)。

The ViewModel is not populated at once (because it can't be), but rather data are loaded into it as the user goes through the app and specifies what data he wants to load (this is mostly based on some timespan and/or other criteria).

我创建了的 MainViewModel 的中的 App.xaml.cs 的是这样的:

I have created the MainViewModel in App.xaml.cs like this:

private static MainViewModel viewModel = null;
public static MainViewModel ViewModel
{
    get
    {
        if (viewModel == null)
        {
            viewModel = new MainViewModel();
        }
        return viewModel;
    }
}

注意:有些人听说MVVM可能/应区别使用,我应该为每一个页面,而不是有一个单例类类从绑定视图模型。考虑之后,我决定让它像我现在拥有它。

我想现在要做的是将视图模型我作为源/ DataContext的,从在XAML

What I would like to do now is to set the viewModel I created as a source/datacontext to bind from in XAML


  1. 如何实现这一目标?

如果我想设置一个的ItemSource 的一个列表框/ longListSelector或文本 PersonalInfoModel 的<$ C $里面看重> C> MainViewModel ,我应该怎么办呢?

If I'd like to set an itemSource of a listBox/longListSelector or Text of the textBox to value in for example PersonalInfoModel inside the MainViewModel, how should i do it?

PS:截至问题的开始是写的说明,我是一个菜鸟。我知道,这很难与我们的时候,但是没有从巨大的虚空只是催生了伟大的思想家,这就是为什么我问的更详细的解释则只是你shlould设置你的对象在窗口中的数据源,然后将该

推荐答案

你问的问题是,基本上是:如何连接查看和视图模型?我完全同意,这是当你开始使用MVVM和,这个问题完全是由许多教程和文章对MVVM忽视解决最令人困惑的问题。

The question you're asking is, basically: How do I connect View and ViewModel? I totally agree that this is the most confusing problem to solve when you're getting started with MVVM and that this question is completely ignored by many tutorials and posts on MVVM.

答案是:有很多方法,你希望它是获得视图模型,那就是在视图的的DataContext 。虽然你可以在XAML中做纯粹的,微软建议设置的DataContext 这样,只要我能记得的WP项目模板:

The answer is: There are many ways to get the ViewModel where you want it to be, that is in the View's DataContext. Though you could do it purely in XAML, Microsoft suggests to set the DataContext like this, as far as I can recall the WP project templates:

在代码隐藏在你浏览的构造,只需拨打:

In your Views constructor in codebehind, simply call:

DataContext = App.MainViewModel;



其中,最有价值的资源,帮助我得到这个问题直是这样的帖子在

One of the most valuable sources that helped me get this problem straight is this post in

保罗Stovell的博客

这是关于WPF,而不是WP8,但它仍应有所帮助。

It's about WPF and not WP8 but it should help nonetheless.

要执行的实际绑定,您现在可以按照教程,例如:

To perform the actual binding, you can now follow the tutorials, for example:

<TextBlock Text="{Binding PersonalInfoModel.Name}" />

这篇关于XAML数据绑定单样MVVM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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