0

我使用PRISM 4,並且幾乎掌握了所有功能,但是隻要我想將我的DomainContext類(RIA)注入到視圖模型中,地獄就會鬆動。 :)如果有經驗的Unity/Prism開發人員可以給我一個建議如何繼續,那將是非常棒的。統一:解決RIA的問題DomainContext

在我的引導程序,我註冊在Unity集裝箱所需的類是這樣的:

protected override void ConfigureContainer() 
     { 
      base.ConfigureContainer(); 
      Container.RegisterType<SCMDomainContext>(); 
     } 

內NavigationModule,在構造函數我有以下的與特定區域註冊NavigationView。

public NavigationModule(IUnityContainer container, IRegionManager regionManager) 
     { 
      _container = container; 
      _regionManager = regionManager; 

      _regionManager.RegisterViewWithRegion(Constants.NavRegion,() => _container.Resolve<NavigationView>()); 

     } 

觀需要視圖模型的依賴性:

public NavigationView(NavigationViewModel viewModel) 
     { 
      InitializeComponent(); 

      Loaded += (s, e) => 
          { 
           DataContext = viewModel; 
          };    
     } 

視圖模型有以下幾點:

public NavigationViewModel(SCMDomainContext context) 
     { 
      _context = context; 
      ConstructCommon(); 
     } 

當我評論這個構造函數,並把一個帶空的構造函數,這一切都很好,出於某種原因,我無法解決SCMDomainContext類。 Ria Services提供的是爲您創建的域上下文。

由於我使用的是Silverlight,我看不到跟蹤異常的堆棧跟蹤,我得到的只是此頁面上的消息。請問我錯過了什麼?

Microsoft JScript runtime error: Unhandled Error in Silverlight Application An exception occurred while initializing module 'NavigationModule'. 
    - The exception message was: Activation error occured while trying to get instance of type NavigationModule, key '' 
    Check the InnerException property of the exception for more information. If the exception occurred 
    while creating an object in a DI container, you can exception.GetRootException() to help locate the 
    root cause of the problem. at Microsoft.Practices.Prism.Modularity.ModuleInitializer.HandleModuleInitializationError(ModuleInfo moduleInfo, String assemblyName, Exception exception) 
    at Microsoft.Practices.Prism.Modularity.ModuleInitializer.Initialize(ModuleInfo moduleInfo) 
    at Microsoft.Practices.Prism.Modularity.ModuleManager.LoadModulesThatAreReadyForLoad() 
    at Microsoft.Practices.Prism.Modularity.ModuleManager.IModuleTypeLoader_LoadModuleCompleted(Object sender, LoadModuleCompletedEventArgs e) 
    at Microsoft.Practices.Prism.Modularity.XapModuleTypeLoader.RaiseLoadModuleCompleted(LoadModuleCompletedEventArgs e) 
    at Microsoft.Practices.Prism.Modularity.XapModuleTypeLoader.HandleModuleDownloaded(DownloadCompletedEventArgs e) 
    at Microsoft.Practices.Prism.Modularity.XapModuleTypeLoader.IFileDownloader_DownloadCompleted(Object sender, DownloadCompletedEventArgs e) 
    at Microsoft.Practices.Prism.Modularity.FileDownloader.WebClient_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e) 
    at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) 
    at System.Net.WebClient.OpenReadOperationCompleted(Object arg) 

你這個幫助是高度讚賞, 卡瓦

回答

0

事實上,它最好像這樣爲DomainContext創建一個圖層。那麼它可以通過IoC輕鬆解決:

public class ContactModuleService : IContactModuleService 
    { 
     readonly SCMDomainContext _context = new SCMDomainContext(); 

     #region Implementation of IContactModuleService 

     public EntitySet<Contact> Contacts 
     { 
      get { return _context.Contacts; } 
     } 

     public EntityQuery<Contact> GetContactsQuery() 
     { 
      return _context.GetContactsQuery(); 
     } 

     public SubmitOperation SubmitChanges(Action<SubmitOperation> callback, object userState) 
     { 
      return _context.SubmitChanges(callback, userState); 
     } 

     public SubmitOperation SubmitChanges() 
     { 
      return _context.SubmitChanges(); 
     } 

     public LoadOperation<TEntity> Load<TEntity>(EntityQuery<TEntity> query, Action<LoadOperation<TEntity>> callback, object userState) where TEntity : Entity 
     { 
      return _context.Load(query, callback, userState); 
     } 

     public LoadOperation<TEntity> Load<TEntity>(EntityQuery<TEntity> query) where TEntity : Entity 
     { 
      return _context.Load(query); 
     } 

     #endregion 
    } 
0

我看不到太多錯在這裏。不過話說回來,我使用的是從接口的初始化方法以下列方式註冊類型和視圖地區:

#region properties 
[Dependency] 
public IUnityContainer Container { get; set; } 

[Dependency] 
public IRegionManager RegionManager { get; set; } 
#endregion 

public virtual void Initialize() 
{ 
    this.Container.RegisterType<NavigationViewModel>(new ContainerControlledLifetimeManager()); 
    this.Container.RegisterType<NavigationView>(new ContainerControlledLifetimeManager()); 

    this.RegionManager.RegisterViewWithRegion(Constants.NavRegion,() => this.Container.Resolve<NavigationView>()); 
} 

不能確定它是否有差別,如果你不明確註冊視圖模型和「查看」類型。就我個人而言,我更喜歡控制容器如何解決類型的方式。