这是在MVVM中使用ViewModelLocator的好方法

Is this a good way of using ViewModelLocator in MVVM

WE have a ViewModelLocater class in our Silverlight App. It consist of a basic constructor and a public property to return the ViewModel for a class. The code is something like this

public class ViewModelLocator
{
    private  Dictionary<string, ViewModel> _viewModels =
              new Dictionary<string, ViewModel>();

    public ViewModelLocator()
    {
        _viewModels.Add("Home",  HomeViewModel());
        _viewModels.Add("Setup", new SetupViewModel());
        _viewModels.Add("TasksActivities", new TasksActivitiesViewModel());
        _viewModels.Add("Timesheet", new TimesheetViewModel());
    }

    public ViewModel this[string viewName]
    {
        get { return _viewModels[viewName]; }
    }
}

and in each of the XAML pages we set the ViewModel for that page using

DataContext="{Binding [Setup], Source={StaticResource ViewModelLocator}}"

Setup is the key in the above dictionary.

The Silverlight App is really big and we have only recently started looking into any memory leaks(There are many...) I am using Windbg to track these leaks and I have noticed a lot of memory leaks leading back to the ViewModelLocater class. Every time the app loads the ViewModelLocator constructor creates ViewModels for all the Views. So I am wondering if there is a better way of implementing the ViewModelLocator class.

We use ViewModelLoader/ViewModelLocator to provide both DesignTime as well as Runtime DataContexts.

ViewModelLocator Class

public static class ViewModelLocator
{
    public static readonly DependencyProperty FactoryProperty = DependencyProperty.RegisterAttached("Factory",
        typeof (IViewModelFactory), typeof (ViewModelLocator),
        new FrameworkPropertyMetadata(null, PropertyChangedCallback));

    public static void SetFactory(DependencyObject dependencyObject, IViewModelFactory value)
    {
        dependencyObject.SetValue(FactoryProperty, value);
    }

    public static IViewModelFactory GetFactory(DependencyObject dependencyObject)
    {
        return (IViewModelFactory) dependencyObject.GetValue(FactoryProperty);
    }

    private static void PropertyChangedCallback(DependencyObject dependencyObject,
        DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        var fe = dependencyObject as FrameworkElement;
        if (fe != null)
        {
            fe.DataContext = GetFactory(dependencyObject).Create();
        }
    }
}

IViewModelFactory

public interface IViewModelFactory
{
    object Create();
}

ViewModelFactory

public class MainViewModelFactory : ViewModelFactoryBase
{
    protected override object CreateDesignTimeViewModel()
    {
        return new MainViewModel(new DesignTimeEventAggregator(), new DesignTimeLogger(), new ViewModelViewRepository());
    }

    protected override object CreateViewModel()
    {
        return ServiceLocator.Current.GetInstance<MainViewModel>();
    }
}

ViewModelFactoryBase Class

public abstract class ViewModelFactoryBase : IViewModelFactory
{
    protected abstract object CreateDesignTimeViewModel();

    protected abstract object CreateViewModel();

    public object Create()
    {
        return Designer.IsInDesignTime() ? CreateDesignTimeViewModel() : CreateViewModel();
    }
}

And in XAML, this is how I hookup ViewModel Locator to View:

<viewModelLocation:ViewModelLocator.Factory>
    <viewModelFactories:MainViewModelFactory />
</viewModelLocation:ViewModelLocator.Factory>