Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MEF容器。GetExportedValue<;T>;()实例化不创建单独的实例_C#_Mef_Caliburn.micro - Fatal编程技术网

C# MEF容器。GetExportedValue<;T>;()实例化不创建单独的实例

C# MEF容器。GetExportedValue<;T>;()实例化不创建单独的实例,c#,mef,caliburn.micro,C#,Mef,Caliburn.micro,我正在尝试从目录中引用视图模型 当我使用Container.GetExportedValue并初始化属性时,所有实例的属性都设置为最终值“p”的值。但是当我使用标准的初始化器时,它们是好的 因此,在我的示例中,MEF实例示例中FormViewModel的Name属性具有以下值 C C C 但在正常情况下,示例具有这些值 A B C 它的行为就像来自MEF容器的所有实例之间存在一些共享引用 var worker = new BackgroundWorker(); wor

我正在尝试从目录中引用视图模型

当我使用Container.GetExportedValue并初始化属性时,所有实例的属性都设置为最终值“p”的值。但是当我使用标准的初始化器时,它们是好的

因此,在我的示例中,MEF实例示例中FormViewModel的Name属性具有以下值

C C C

但在正常情况下,示例具有这些值

A B C

它的行为就像来自MEF容器的所有实例之间存在一些共享引用

      var worker = new BackgroundWorker();
        worker.DoWork += (o, ea) =>
                         {
                             _forms = new ObservableCollection<FormViewModel>(
                                 FormsExplorerRepository.GetForms()
                                     .Select(p =>
                                             {
 // This way of instancing does strange stuff 
                                                 var fvm = Container.GetExportedValue<FormViewModel>();

// This is fine but of course I'm not getting the importing constructor called
                                                 var fvm = new FormViewModel();

                                                 fvm.Workspace = this;
                                                 fvm.FormId = p.FormId;
                                                 fvm.Label = p.Label;
                                                 fvm.Name = p.Name;
                                                 fvm.Disclaimer = p.Disclaimer;
                                                 fvm.CertificationText = p.CertificationText;
                                                 fvm.Schemes = FormViewModelExtensions.InitialiseSchemes(p);
                                                 return fvm;
                                             })
                                     .ToList());
                         };
我在类定义上有一个导出属性

 [Export(typeof(FormViewModel)), PartCreationPolicy(CreationPolicy.NonShared)]
public class FormViewModel 
我希望这里有足够的信息可以帮助别人

我发现了我的错误

我在AddExportedValue中没有使用正确的语法(我在这里注释的语法是错误的)

(容器、批次)=>
{
//batch.AddExportedValue(新FormViewModel());
batch.AddExportedValuecontainer.GetExportedValue);
}
我发现了我的错误

我在AddExportedValue中没有使用正确的语法(我在这里注释的语法是错误的)

(容器、批次)=>
{
//batch.AddExportedValue(新FormViewModel());
batch.AddExportedValuecontainer.GetExportedValue);
}
 [Export(typeof(FormViewModel)), PartCreationPolicy(CreationPolicy.NonShared)]
public class FormViewModel 
(container, batch) =>
{
    // batch.AddExportedValue(new FormViewModel());
    batch.AddExportedValue<Func<FormViewModel>>container.GetExportedValue<FormViewModel>);
}