我写了一个非常简单的 ViewModel Locator :
public static readonly DependencyProperty LocateForProperty =
DependencyProperty.RegisterAttached("LocateFor",
typeof (Object),
typeof (ViewModelLocator),
new PropertyMetadata(null, OnLocateForChanged));
public static object GetLocateFor(DependencyObject dependencyObject)
{
return dependencyObject.GetValue(LocateForProperty);
}
public static void SetLocateFor(DependencyObject dependencyObject, Object value)
{
dependencyObject.SetValue(LocateForProperty, value);
}
private static void OnLocateForChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
{
if (dependencyObject != null)
{
var viewModelName = dependencyObject.GetType().AssemblyQualifiedName;
if (! string.IsNullOrEmpty(viewModelName))
{
viewModelName = viewModelName.Replace("View", "ViewModel");
var viewModel = DependencyLocator.GetInstance(Type.GetType(viewModelName));
if (viewModel != null)
{
((FrameworkElement) dependencyObject).DataContext = viewModel;
}
}
}
}
并像这样使用它:
<vm:ViewModelLocator.LocateFor>
<Binding RelativeSource="{RelativeSource Self}" />
</vm:ViewModelLocator.LocateFor>
它在运行时完美运行,但我丢失了设计时间位置。我怀疑这是因为我使用的是相对绑定,我尝试将其更改为x:Static
但不起作用。是否有任何建议的更改可以让我恢复可混合性?
编辑:关于 DependencyLocator,它是 Ninject 的静态包装器:
using System;
using System.Collections.Generic;
/// <summary>
/// Defines an object that wraps around specific DI containers.
/// </summary>
public static class DependencyLocator
{
/// <summary>
/// Gets or Sets the function used for locating a single instance of a specifc type.
/// </summary>
public static Func<Type, object> GetInstance;
/// <summary>
/// Gets or Sets the function used for locating all instance of a specific type.
/// </summary>
public static Func<Type, IEnumerable<object>> GetAllInstances;
/// <summary>
/// Gets the implementation of the provided service.
/// </summary>
public static T Get<T>()
{
return (T)GetInstance(typeof(T));
}
/// <summary>
/// Gets all implementations of the provided service.
/// </summary>
public static IEnumerable<T> GetAll<T>()
{
return (IEnumerable<T>) GetAllInstances(typeof (T));
}
}
它被填充到 app.xaml 的 app_start 类中。出于设计时的目的,它是这样设计的,如果我使用 View 的 xaml 中定义的实例对象创建一个简单的视图模型定位器类,它确实从 DependencyLocator 获得了正确的值,只是因为我切换到了依赖属性,所以这个问题上来了。