Need help setting up MVVM Light ViewModelLocator

删除回忆录丶 提交于 2019-12-11 02:29:41

问题


As the title says, I need some help setting up my ViewModelLocator. It's a Windows Phone 7 app that uses the Galasoft MVVM Light Toolkit.

I have a second page in my app which I need to wire up to the view model, but after looking strenuously on the internet for the past hour, haven't really been able to find a simple example of adding ViewModels to the locator. All I need is a code example of what I need to add to the locator and whereabouts to add it.

Thanks to anyone who can help me with this.


回答1:


This is my ViewModelLocator:

public class ViewModelLocator
{
    public MainPageViewModel MainPage
    {
        get { return new MainPageViewModel(); }
    }
}

This is a piece of my App.xaml:

<Application.Resources>
    <vm:ViewModelLocator
        x:Key="ViewModelLocator" />
</Application.Resources>

This is a piece of my page xaml:

DataContext="{Binding MainPage, Source={StaticResource ViewModelLocator}}"


来源:https://stackoverflow.com/questions/8075305/need-help-setting-up-mvvm-light-viewmodellocator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!