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

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

问题描述

我是新手,有时我会陷入简单和/或愚蠢的想法中,这就是其中之一.

我对数据绑定有了大致的了解,我已经浏览了网上的一些教程,并在谷歌上搜索了几个小时的大量文本,这让我有点困惑.

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 应用程序.

使用提供给我的网络服务和一些方法,我正在加载我需要查看的数据(有时以不同的组合),并且我需要在应用程序运行时存储其中的大部分数据.

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.

  • 为此,我创建了一个 ViewModel + 多个模型,并将它们构建成这样:

  • For that purpose I have created a ViewModel + several Models and structured them like that:

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

ViewModel 不会立即填充(因为它不能),而是在用户通过应用程序并指定什么时将数据加载到其中他想要加载的数据(这主要基于某个时间跨度和/或其他标准).

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

我在 App.xaml.cs 中创建了 MainViewModel,如下所示:

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 可能/应该以不同的方式使用,我应该为每个页面创建一个 ViewModel,而不是绑定一个类似单例的类.经过一番考虑,我决定让它就像我现在拥有的一样.

我现在想要做的是将我创建的 viewModel 设置为要在 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. 如何实现这一目标?

如果我想将 textBox 的 listBox/longListSelector 的 itemSourceText 设置为例如 的值MainViewModel里面的PersonalInfoModel,我该怎么做?

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?

P.S.:正如问题开头的注释中所写,我是菜鸟.我知道有时对我们来说很难,但是没有一个伟大的思想家刚刚从巨大的空白中产生,这就是为什么我要求更详细的解释然后只是你应该将你的对象设置为窗口中的数据源,然后设置这个".

推荐答案

您要问的问题基本上是:如何连接 View 和 ViewModel?我完全同意,这是当您开始使用 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.

答案是:有很多方法可以将 ViewModel 放在您想要的位置,即在 View 的 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

Paul 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天全站免登陆