XAML Databinding singleton-like MVVM object

2019-02-15 11:42发布

NOTES

I'm a rookie, I sometimes get stuck at simple and/or stupid thinks, this being one of them.

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.

PROBLEM

I'm working on Windows Phone 8 C#/XAML .NET 4.5 Application.

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.

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

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


  • 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;
        }
    }
    

    NOTE: From some people I heard the MVVM might/should be used differently and I should create a ViewModel for every page instead of having one singleton-like class to bind from. After a consideration, I have decided to let it be like I have it now.


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

QUESTIONS

  1. How to achieve this?

  2. 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.: As is written in the note at the start of the question, I'm a rookie. I know that it's hard with us sometimes, but none of the great thinkers just spawned from the great void, thats why I'm asking for a more detailed explanation then just "You shlould set your object as datasource in the window and then set this".

1条回答
闹够了就滚
2楼-- · 2019-02-15 12:26

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.

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's Blog

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}" />
查看更多
登录 后发表回答