简体   繁体   English

初始化时使用MEF在Prism中查看时遇到问题

[英]View in Prism using MEF having problems while initializing

I have created a small demo application using WPF Prism and I am using Mef. 我已经使用WPF Prism创建了一个小型演示应用程序,并且正在使用Mef。

Here is the Shell of the Application: 这是应用程序的外壳:

<Window ..........>
    <Grid>
        <ContentControl 
            prism:RegionManager.RegionName="{x:Static inf:RegionNames.ContentRegion}" />
    </Grid>
</Window>

Here ContentRegion is just a static string defined in another class of infrastructure project. 这里ContentRegion只是在另一类基础结构项目中定义的静态字符串。

Here is my Bootstrapper class: 这是我的Bootstrapper类:

public class Bootstrapper : MefBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        return Container.GetExportedValue<Shell>();
    }

    protected override void InitializeShell()
    {
        base.InitializeShell();

        App.Current.MainWindow = (Window)Shell;
        App.Current.MainWindow.Show();
    }

    protected override void ConfigureAggregateCatalog()
    {
        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(RegionNames).Assembly));
        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleMyModule).Assembly));
    }
}

As you can see I have added my main executing project and its Infrastructure project to this Bootstrapper. 如您所见,我已将我的主要执行项目及其基础结构项目添加到该Bootstrapper中。

Now I have created a very simple Module called MyModule. 现在,我创建了一个非常简单的模块MyModule。 It has a class called ModuleMyModule: 它有一个名为ModuleMyModule的类:

[ModuleExport(typeof(ModuleMyModule), InitializationMode = InitializationMode.WhenAvailable)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ModuleMyModule : IModule
{
    IRegionManager _regionManager;

    [ImportingConstructor]
    public ModuleMyModule(IRegionManager regionManager)
    {
        _regionManager = regionManager;
    }

    public void Initialize()
    {
        _regionManager.RegisterViewWithRegion(RegionNames.ContentRegion, typeof(MyView));
    }
}

Now, I have a View called MyView in this application as follows: 现在,我在此应用程序中有一个名为MyView的视图,如下所示:

<UserControl ............>
    <Grid>
        <CheckBox Content="Have you Checked it properly?"/>
    </Grid>
</UserControl>

Upto this point my application works fine. 到目前为止,我的应用程序运行正常。

The problem starts now: 现在开始出现问题:

Now, I added a ViewModel to this project. 现在,我向该项目添加了ViewModel。 So, now my view MyView looks like: 所以,现在我的视图MyView看起来像:

<UserControl ............>
    <Grid>
        <CheckBox IsChecked="{Binding IsProperlyChecked}" Content="Have you Checked it properly?"/>
    </Grid>
</UserControl>

And Here is the .cs file of MyView: 这是MyView的.cs文件:

[Export(typeof(MyView))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class MyView : UserControl, IView
{
    [ImportingConstructor]
    public MyView(IMyViewModel viewModel)
    {
        InitializeComponent();
        ViewModel = viewModel;
    }

    public IViewModel ViewModel
    {
        get
        {
            return (IViewModel)DataContext;
        }
        set
        {
            DataContext = value;
        }
    }
}

Here is my ViewModel class: 这是我的ViewModel类:

[Export(typeof(MyViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MyViewModel : IMyViewModel, INotifyPropertyChanged
{
    [ImportingConstructor]
    public MyViewModel()
    {
        IsProperlyChecked = true;
    }

    private bool _IsProperlyChecked;

    public bool IsProperlyChecked
    {
        get
        {
            return _IsProperlyChecked;
        }
        set
        {
            if (_IsProperlyChecked != value)
            {
                _IsProperlyChecked = value;
                OnPropertyChanged("IsProperlyChecked");
            }
        }
    }

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

IMyViewModel is an interface as shown below: IMyViewModel是一个界面,如下所示:

public interface IMyViewModel : IViewModel
{
    bool IsProperlyChecked { get; set; }
}

Now, my project stops working: 现在,我的项目停止工作:

I am getting an error: 我收到一个错误:

An exception has occurred while trying to add a view to region 'ContentRegion'. 

    - The most likely causing exception was was: 'Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type MyView, key "" ---> Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type MyView, key ""

   at Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type serviceType, String key)

   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)

   --- End of inner exception stack trace ---

   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)

   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType)

   at Microsoft.Practices.Prism.Regions.RegionViewRegistry.CreateInstance(Type type)

   at Microsoft.Practices.Prism.Regions.RegionViewRegistry.<>c__DisplayClass1.<RegisterViewWithRegion>b__0()

   at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.OnViewRegistered(Object sender, ViewRegisteredEventArgs e)'.

Why this exception is thrown?? 为什么抛出此异常?

I think I am doing something wrong. 我想我做错了。 I am very new to MEF, I used Unity in past. 我对MEF非常陌生,我过去曾经使用过Unity。

There I need to register ViewModel and its interface. 在那里,我需要注册ViewModel及其接口。 But I don't know if its required in MEF. 但是我不知道MEF是否需要它。 If required then How??? 如果需要的话,如何???

Demo Project : 示范项目:

https://drive.google.com/file/d/0Bw2XAE1EBI6rU3VsYjVyQmhFRFE/view?usp=sharing https://drive.google.com/file/d/0Bw2XAE1EBI6rU3VsYjVyQmhFRFE/view?usp=sharing

When using MEF a type passed as a parameter in ExportAttribute should match the type expected by import(s). 使用MEF时,在ExportAttribute作为参数传递的类型应与import所需的类型匹配。

Since MyView constructor requires IMyViewModel type: 由于MyView构造函数需要IMyViewModel类型:

 [ImportingConstructor]
 public MyView(IMyViewModel viewModel)
 {
 ..

... try exporting MyViewModel class as IMyViewModel ...尝试将MyViewModel类导出为IMyViewModel

 [Export(typeof(IMyViewModel))] 
 [PartCreationPolicy(CreationPolicy.NonShared)]
 public class MyViewModel : IMyViewModel, INotifyPropertyChanged
 {
  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM