如何从后面的代码访问ViewModelLocator中的MainViewModel? [英] How do you access the MainViewModel in ViewModelLocator from code behind?

查看:279
本文介绍了如何从后面的代码访问ViewModelLocator中的MainViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVVM light为我的视图模型构建WP7应用.我正在使用通过NuGet添加库时添加的ViewModelLocator.效果很好,但现在我需要从代码访问ViewModel.

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.

在我的代码中,用户单击一个按钮,我需要搜索MainViewModel(其中包含多个视图模型)并根据用户输入的条件找到一个.

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.

通常,我只是响应按钮的Click事件,但没有ViewModelLocator类的实例变量来获取MainViewModel的执行权限.使用Windows Phone 7的默认模板(非MVVMLight),App类对主视图模型具有静态变量,因此您可以随时通过App.ViewModel进行访问.

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.

twitter上有一些关于使用命令的讨论,但是有时我必须在多个vm上执行代码搜索以获得所需的结果.可能需要将ISearchViewModel服务注入到View或其他工具中才能使其正常工作.

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.

这是所提供的ViewModelLocator的实现:

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

因此,从另一个视图后面的代码中,如何访问Main(MainViewModel包含所有数据列表,并具有我调用的搜索方法)?还是应该?

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?

谢谢.

推荐答案

如果像在模板中那样创建ViewModelLocator,则对ViewModels有静态引用. mvvmlocatorproperty -snippet创建像这样的ViewModel属性. 这意味着您可以实例化一个新的ViewModelLocator来在单击按钮后的代码中找到ViewModels.它将始终是相同的视图模型,而不依赖于ViewModelLocator的不同实例

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

这篇关于如何从后面的代码访问ViewModelLocator中的MainViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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