English 中文(简体)
模块中的棱晶、团结和违约类型登记
原标题:Prism, Unity and default type registration in Modules

Technologies

  • C# 4.0
  • Prism 4 with Unity for DI
  • WPF
  • MVVM

Preface

我的解决办法有两个项目:MyApp.ShellMyApp.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.

最佳回答

// Somehow, Unity sees this class and registers the type. public class FooViewModel : ViewModelBaseClass ...

我感到惊讶的是,你说这番话,因为团结组织没有在集装箱内按违约登记类型。 你们必须告诉我们,无论从方案上还是从编织文件中都这样做。

When you have concrete classes (not interfaces) they will automatically get created by Unity whether they are registered or not. If not the default behavior is to create a new instance each time. No lifetime management is applied also.

As far as your questions:

在你刚刚开始的单元中只登记一种类型。

Container.RegisterType<FooViewModel>(new ContainerControlledLifetimeManager());

终身管理人员将指示团结,只创造一种观点模式。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签