簡體   English   中英

初始化時使用MEF在Prism中查看時遇到問題

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

我已經使用WPF Prism創建了一個小型演示應用程序,並且正在使用Mef。

這是應用程序的外殼:

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

這里ContentRegion只是在另一類基礎結構項目中定義的靜態字符串。

這是我的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));
    }
}

如您所見,我已將我的主要執行項目及其基礎結構項目添加到該Bootstrapper中。

現在,我創建了一個非常簡單的模塊MyModule。 它有一個名為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));
    }
}

現在,我在此應用程序中有一個名為MyView的視圖,如下所示:

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

到目前為止,我的應用程序運行正常。

現在開始出現問題:

現在,我向該項目添加了ViewModel。 所以,現在我的視圖MyView看起來像:

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

這是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;
        }
    }
}

這是我的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是一個界面,如下所示:

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

現在,我的項目停止工作:

我收到一個錯誤:

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)'.

為什么拋出此異常?

我想我做錯了。 我對MEF非常陌生,我過去曾經使用過Unity。

在那里,我需要注冊ViewModel及其接口。 但是我不知道MEF是否需要它。 如果需要的話,如何???

示范項目:

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

使用MEF時,在ExportAttribute作為參數傳遞的類型應與import所需的類型匹配。

由於MyView構造函數需要IMyViewModel類型:

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

...嘗試將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