Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access the MainViewModel in ViewModelLocator from code behind?

Building a WP7 app using MVVM light for my view models. I'm using the ViewModelLocator that gets added when you add the library through NuGet. Works great but now I need to get access to a ViewModel from code.

In my code the user clicks a button and I need to search the MainViewModel (which contains several view models) and find one based on the criteria the user entered.

Normally I would just response to the Click event of the button but I don't have an instance variable of the ViewModelLocator class to get a hold of the MainViewModel to perform the search. With the default template (non-MVVMLight) for Windows Phone 7, the App class has a static variable to the main view model so you can access it anytime with App.ViewModel.

There's some talk from twitter about using commands which would be good, but at some point I have to perform a code search across multiple vms to get the results I need. Probably need to inject a ISearchViewModel service into the View or something to make this work.

Here's the implementation of ViewModelLocator that is provided:

public class ViewModelLocator
{
    private static MainViewModel _main;

    public ViewModelLocator()
    {
        _main = new MainViewModel();
    }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
    "CA1822:MarkMembersAsStatic",
    Justification = "This non-static member is needed for data binding purposes.")]
    public MainViewModel Main
    {
        get
        {
            return _main;
        }
    }
}

So from the code behind of another view, how do you get access to Main (MainViewModel contains all the lists of data and has a search method I call)? Or Should you?

Just wondering how people are solving this type of problem?

Thanks.

like image 424
Bil Simser Avatar asked Nov 30 '22 07:11

Bil Simser


2 Answers

In MVVM-Light the ViewModelLocator is provided as an application resource. Therefore you can still directly access it, but the syntax is different. If you look at your App.xaml you should see this piece of code somewhere.

<Application.Resources>
    <vm:ViewModelLocator x:Key="Locator"
                         d:IsDataSource="True" />
</Application.Resources>

From anywhere in your application you can access the App's resources and therefore also the MainViewModel with this piece of code:

(App.Current.Resources["Locator"] as ViewModelLocator).Main

This works for any application resource.

like image 155
Tom Verhoeff Avatar answered Dec 18 '22 10:12

Tom Verhoeff


If you created the ViewModelLocator as in the template you have static references to the ViewModels. The mvvmlocatorproperty-snippet creates ViewModel-properties like this. This means that you could just instantiate a new ViewModelLocator to locate the ViewModels in your code behind button click. It will always be the same viewmodels independent of the different instances of the ViewModelLocator

like image 23
Martin Avatar answered Dec 18 '22 09:12

Martin