我的ViewModel构造函数中的一个 View 需要一个DependencyProperty:
我的问题:MEF不会SatisfyImports(),因为它被标记为一个或多个ExportAttributes(异常(exception))
这是VIEW的代码结构:
public class MyView : UserControl
{
[Export(MethodTypes.ChartType)]
public Charts MyChartType
{
get
{
object k = GetValue(ChartTypeProperty);
Charts f = (Charts)Enum.Parse(typeof(Charts), k.ToString(), true);
return f;
}
set
{
SetValue(ChartTypeProperty, value);
}
}
[Import(ViewModelTypes.GenericChartViewModel)]
public object ViewModel
{
set
{
DataContext = value;
}
}
public MyView()
{
InitializeComponent();
if (!ViewModelBase.IsInDesignModeStatic)
{
// Use MEF To load the View Model
CompositionInitializer.SatisfyImports(this);
}
}
}
和VIEWMODEL:
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(ViewModelTypes.GenericChartViewModel)]
public class GenericChartViewModel
{
[ImportingConstructor]
public GenericChartViewModel([Import(MethodTypes.ChartType)] Charts forChartType)
{
string test = forChartType.ToString();
}
}
请给我任何提示,或者为通过mef传递参数提供更好的解决方案
就我而言,我现在只需要通过dependecyproperty ...
谢谢
最佳答案
您的工作不是很好。.您不能从ChartTypes中删除导出并将其手动传递给任何需要的人吗?我认为viewmodel只是其中之一。
关于silverlight - Silverlight View通过MEF将DependencyProperty导出到ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4324809/