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

时间:2013-11-20 06:14:23

标签: silverlight xaml mvvm viewmodellocator

我们的Silverlight应用程序中有一个ViewModelLocater类。它由一个基本构造函数和一个公共属性组成,用于返回类的ViewModel。代码是这样的

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

在每个XAML页面中,我们使用

为该页面设置ViewModel
DataContext="{Binding [Setup], Source={StaticResource ViewModelLocator}}"

Setup是上述词典中的关键。

Silverlight应用程序真的很大,我们最近才开始研究任何内存泄漏(有很多......)我使用Windbg来跟踪这些泄漏,我注意到很多内存泄漏导致ViewModelLocater类。每次应用加载ViewModelLocator构造函数时,都会为所有视图创建ViewModel。所以我想知道是否有更好的方法来实现ViewModelLocator类。

1 个答案:

答案 0 :(得分:1)

我们使用ViewModelLoader / ViewModelLocator来提供DesignTime和Runtime DataContexts。

ViewModelLocator类

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类

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

    protected abstract object CreateViewModel();

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

在XAML中,这就是我将ViewModel Locator连接到View的方式:

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