Technologies
- C# 4.0
- Prism 4 with Unity for DI
- WPF
- MVVM
Preface
我的解决办法有两个项目:MyApp.Shell和MyApp.ModuleFoo。
MyApp.Shell s Unity Boots Capper
protected override IModuleCatalog CreateModuleCatalog()
{
// Module assemblies are read from a directory.
DirectoryModuleCatalog moduleCatalog = new DirectoryModuleCatalog();
moduleCatalog.ModulePath = @".Modules";
return moduleCatalog;
}
项目MyApp.ModuleFoo载有观点和观点模式。
www.un.org/Depts/DGACM/index_spanish.htm The ViewModel
// Somehow, Unity sees this class and registers the type.
public class FooViewModel : ViewModelBaseClass
{
public string FooText
{
get { return "Foo!"; }
}
}
www.un.org/Depts/DGACM/index_spanish.htm The View
<Label Content={Binding FooText} />
www.un.org/Depts/DGACM/index_spanish.htm The View s Code-behind
// Unity automatically sees this as Constructor Injection,
// which is exactly what I desire.
public FooView(FooViewModel viewModel)
{
DataContext = viewModel;
...
}
<>MyApp.Foo Module s initialization
也许向地区管理人员登记FooView无意中向团结组织登记FooViewModel?
public void Initialize()
{
var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
regionManager.RegisterViewWithRegion("FooRegion", typeof(FooView));
}
The view correctly displays "Foo!".
Problems
- How do I tell Unity to register only a single instance of FooViewModel?
- Additionally, (and I m thinking ahead here), how would I tell unity not to register FooViewModel?
Thanks for the help.
Edit:
添加MyApp.Foo Module 初步化法
Edit (Solution):
It turns out RegisterViewWithRegion has two overloads. From Prism s documentation, when the overload I m using is used, a new instance of the view is created. I m assuming this also creates a new instance of FooViewModel.
The other overload uses a delegate to resolve FooView. The documentation says this overload is used in the "ViewModel-first" approach. I m going to make this question as answered, but if anyone has any additional insight, I d love to hear.